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;
}
How to list all the folders and its files in php [duplicate]
Question
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
OTHER TIPS
while scanning directory you only want mp3 files. try directory iterator
$scan_it = new RecursiveDirectoryIterator("/example_dir");
foreach(new RecursiveIteratorIterator($scan_it) as $file) {
if (strtolower(substr($file, -4)) == ".mp3") {
echo $file;
}
}
function Read_Dir($dir) {
$dh = opendir($dir);
$files = array();
while (($file = readdir($dh)) !== false) {
$flag = false;
if ($file !== '.' && $file !== '..') {
// check here if file is a directory then use it recursively
$files[] = trim($file);
}
}
return $files;
}
Hope it helps
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow