I'm late to the party, but I like to offer my solution with readdir() , not glob() . What I missed in the solution is a recursive version of your solution. But with readdir it is faster than with glob.
So with glob it will look like this:
function myglobdir($path, $level = 0) { $dirs = glob($path.'/*', GLOB_ONLYDIR); $files = glob($path.'/*'); $all2 = array_unique(array_merge($dirs, $files)); $filter = array($path.'/Thumbs.db'); $all = array_diff($all2,$filter); foreach ($all as $target){ echo "$target<br />"; if(is_dir("$target")){ myglobdir($target, ($level+1)); } } }
And this one is with readdir, but it basically has the same result:
function myreaddir($target, $level = 0){ $ignore = array("cgi-bin", ".", "..", "Thumbs.db"); $dirs = array(); $files = array(); if(is_dir($target)){ if($dir = opendir($target)){ while (($file = readdir($dir)) !== false){ if(!in_array($file, $ignore)){ if(is_dir("$target/$file")){ array_push($dirs, "$target/$file"); } else{ array_push($files, "$target/$file"); } } } //Sort sort($dirs); sort($files); $all = array_unique(array_merge($dirs, $files)); foreach ($all as $value){ echo "$value<br />"; if(is_dir($value)){ myreaddir($value, ($level+1)); } } } closedir($dir); } }
I hope someone finds this helpful.
source share