![]() |
Best way to get files from a dir filtered by certain extension in php
So right now I have a directory and I am getting a list of files
Code:
$dir_f = "whatever/random/"; |
PHP's glob() function let's you specify a pattern to search for. PHP has a great function to help you capture only the files you need. Its called glob()
glob - Find pathnames matching a pattern Here is an example usage - Code:
$files = array(); |
Code:
<?phpCode:
//path to directory to scan |
8
down vote If you want more than one extension searched, then preg_grep() is an alternative for filtering: Code:
$files = preg_grep('~\.(jpeg|jpg|png)$~', scandir($dir_f));or glob("{$dir}*.{jpg,*jpeg,gif,ico,png}", GLOB_BRACE). This would work as well for multiple extensions. |
| All times are GMT +8. The time now is 12:25 AM. |
Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.