scandir

How to get only images using scandir in PHP?

♀尐吖头ヾ 提交于 2019-11-26 20:16:40
Is there any way to get only images with extensions jpeg , png , gif etc while using $dir = '/tmp'; $files1 = scandir($dir); You can use glob $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE); If you need this to be case-insensitive, you could use a DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos , fnmatch or pathinfo to get the extension is up to you. Rajesh Kumar R The actual question was using scandir and the answers end up in glob. There is a huge difference in

Exclude hidden files from scandir

情到浓时终转凉″ 提交于 2019-11-26 16:57:50
问题 I am using the following code to get a list of images in a directory: $files = scandir($imagepath); but $files also includes hidden files. How can I exclude them? 回答1: On Unix, you can use preg_grep to filter out filenames that start with a dot: $files = preg_grep('/^([^.])/', scandir($imagepath)); 回答2: I tend to use DirectoryIterator for things like this which provides a simple method for ignoring dot files: $path = '/your/path'; foreach (new DirectoryIterator($path) as $fileInfo) { if(

scandir() to sort by date modified

筅森魡賤 提交于 2019-11-26 07:38:42
问题 I\'m trying to make scandir(); function go beyond its written limits, I need more than the alpha sorting it currently supports. I need to sort the scandir(); results to be sorted by modification date. I\'ve tried a few solutions I found here and some other solutions from different websites, but none worked for me, so I think it\'s reasonable for me to post here. What I\'ve tried so far is this: function scan_dir($dir) { $files_array = scandir($dir); $img_array = array(); $img_dsort = array();

How to get only images using scandir in PHP?

試著忘記壹切 提交于 2019-11-26 07:33:04
问题 Is there any way to get only images with extensions jpeg , png , gif etc while using $dir = \'/tmp\'; $files1 = scandir($dir); 回答1: You can use glob $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE); If you need this to be case-insensitive, you could use a DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos , fnmatch or pathinfo to get the extension is up to you. 回答2: