Tree Function in PHP, Show all files and directories in a directory in PHP

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 in PHP, Show all files and directories in a directory in PHP



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;
}
Tree Function in PHP, Show all files and directories in a directory in PHP Tree Function in PHP, Show all files and directories in a directory in PHP Reviewed by Awais on 15:54 Rating: 5