【发布时间】:2014-11-12 15:01:30
【问题描述】:
我已经设置了一个基本脚本,它发布一组路径以在其中查找模板文件;目前它只搜索两个级别的深度,我在理解一个广泛循环的逻辑以迭代所有子目录直到长度为 0 时遇到了一些麻烦。
如果我有这样的结构:
./components
./components/template.html
./components/template2.html
./components/side/template.html
./components/side/template2.html
./components/side/second/template.html
./components/side/second/template2.html
./components/side/second/third/template.html
./components/side/second/third/template2.html
当理想情况下我希望它检查所有子目录和传递的 .html 文件目录时,它只会在“side”目录中搜索 .html 文件。到目前为止,这是我的工作代码:
<?php
function getFiles($path){
$dh = opendir($path);
foreach(glob($path.'/*.html') as $filename){
$files[] = $filename;
}
if (isset($files)) {
return $files;
}
}
foreach ($_POST as $path) {
foreach (glob($path . '/*' , GLOB_ONLYDIR) as $secondLevel) {
$files[] = getFiles($secondLevel);
}
$files[] = getFiles($path);
}
sort($files);
print_r(json_encode($files));
?>
【问题讨论】: