PHP List Directories Recursively Issue -
i'm trying list php files in specified directory , recursively check sub-directories until finds no more, there numerous levels.
the function have below works fine exception recurses down 1 level.
i've spent hours trying see i'm going wrong, i'm calling scanfiles() when finds new directory seems work 1 level down , stop, appreciated.
updated:
function scanfiles($pparentdirectory) { $vfilearray = scandir($pparentdirectory); $vdirectories = array(); foreach ($vfilearray $vkey => $vvalue) { if (!in_array($vvalue, array('.', '..')) && (strpos($vvalue, '.php') || is_dir($vvalue))) { if (!is_dir($vvalue)) $vdirectories[] = $vvalue; else { $vdirectory = $vvalue; $vsubfiles = scanfiles($vdirectory); foreach ($vsubfiles $vkey => $vvalue) $vdirectories[] = $vdirectory.directory_separator.$vvalue; } } } return $vdirectories; }
you can this:
// helper function function getfiles(&$files, $dir) { $items = glob($dir . "/*"); foreach ($items $item) { if (is_dir($item)) { getfiles($files, $item); } else { if (end(explode('.', $item)) == 'php') { $files[] = basename($item); } } } } // usage $files = array(); getfiles($files, "mydir"); // debug var_dump($files);
mydir looks this: has php files in dirs
output:
p.s. if want function return full path found .php
files, remove basename()
line:
$files[] = basename($item);
this produce result this:
hope helps.
Comments
Post a Comment