PHP Function: dir_list_images()
This function will make an array of all the images in a directory, it will even include all subdirectories by default.
Again, I built this function for a friend who wanted to batch modify all the images using ImageMagick / GD2.
function dir_list_images($dir,$sub=true) { $array = Array(); if (!is_dir($dir)) return false; $od = @opendir($dir); while($file = readdir($od)){ if (substr($file,0,1) == ".") { continue; } elseif($sub && is_dir($dir.$file)) { array_push($array, dir_list_images($dir.$file."/")); } elseif (!is_dir($dir.$file)) && preg_match('/(.jpg|.jpeg|.png|.gif)$/si', $file)) { array_push($array, $dir.$file); } } closedir($od); }
A good example of how this could be used,
this would output an array of all the files:
// Finds all the images from within the current directory // __FILE__ is a global variable that requires PHP 5.0+ $files = dir_list_images( dirname( __FILE __ ) .'/' ); print_r($files); // Outputs: [0] = "/dir/img.png", [1] = "/dir2/img2.jpg" echo count($files); // (Total number) Output: 2
Would be interested to know if you use this.
If you need a conversion for listing images on windows, let me know.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments
No comments yet.
Leave a comment