我有一个文件夹,其中包含目录。一些目录包含文件,另一些包含另一个目录及其文件。我想要的是列出文件夹中的所有文件。假设我的文件夹是 A,它包含文件夹 B 和CB 包含一些 mp3 文件,在 C 中有另一个文件夹 D,在 D 中有一些 mp3 文件。如何列出 B 和 D 中的所有 mp3 文件。请帮助。
问问题
917 次
3 回答
1
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;
}
于 2013-12-11T10:09:17.750 回答
0
在扫描目录时,您只需要 mp3 文件。尝试目录迭代器
$scan_it = new RecursiveDirectoryIterator("/example_dir");
foreach(new RecursiveIteratorIterator($scan_it) as $file) {
if (strtolower(substr($file, -4)) == ".mp3") {
echo $file;
}
}
于 2013-12-11T10:12:46.277 回答
-1
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;
}
希望能帮助到你
于 2013-12-11T10:16:33.500 回答