We use Tree function in Widnows Command Prompt to show all the directories and files in certain directory. But in PHP there is not default function to show or to get all files and directories so i have write a function that is similar to CMD TREE function. And by using this function you can get all files and directories from a path.
tree(); function.
How to use this function:
$tree = tree("your desired path");
function tree($path, $exclude = array()) {
$files = array();
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $splfileinfo) {
if (!in_array(basename($splfileinfo), $exclude)) {
$files[] = str_replace($path, '', '' . $splfileinfo);
}
}
return $files;
}
Questions & comments
Did this work for you, or did you hit a different error? Ask below and we will help.