Search code examples
phpfilesortingdirectoryscandir

How to list all the folders and its files in php


I have a folder and it contains directories in it.Some of the directories contains files and some other contains another directory with its file.What I want is list all the files from the folder.Suppose my folder is A and it contains folder B and C.B contains some mp3 files and in C there is another folder D and in D there are some mp3 files.How it possible to list all mp3 files from B and D.Please help.


Solution

  • function find_all_files($dir) 
    { 
        $root = scandir($dir); 
        foreach($root as $value) 
        { 
            if($value === '.' || $value === '..') {continue;} 
            if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;} 
            foreach(find_all_files("$dir/$value") as $value) 
            { 
                $result[]=$value; 
            } 
        } 
        return $result; 
    }