使用php列出目录中的所有文件夹子文件夹和文件
问题描述:
请给我一个使用php列出目录中所有文件夹,子文件夹,文件的解决方案.我的文件夹结构是这样的:
Please give me a solution for listing all the folders,subfolders,files in a directory using php. My folder structure is like this:
Main Dir
Dir1
SubDir1
File1
File2
SubDir2
File3
File4
Dir2
SubDir3
File5
File6
SubDir4
File7
File8
我想获取每个文件夹中所有文件的列表.
I want to get the list of all the files inside each folder.
php中是否有任何shell脚本命令?
答
function listFolderFiles($dir){
$ffs = scandir($dir);
unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);
// prevent empty ordered elements
if (count($ffs) < 1)
return;
echo '<ol>';
foreach($ffs as $ff){
echo '<li>'.$ff;
if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
echo '</li>';
}
echo '</ol>';
}
listFolderFiles('Main Dir');