【发布时间】:2019-12-21 17:47:35
【问题描述】:
要先扫描根文件夹,将找到的目录存储在数组中(级别 1)。 然后扫描我找到的每个子目录并将它们存储在级别 1 中,作为级别 2 等等。
我怎样才能做到这一点,最多 5 个级别,并在屏幕上显示为文件夹树。
我编写了一个糟糕的代码,有很多错误,但只能正常工作:
$dir = "../pdoks"; //root folder
$szint1 = scandir($dir); //szint1 means LEVEL 1
unset($szint1[array_search('.', $szint1, true)]); //remove dots
unset($szint1[array_search('..', $szint1, true)]);
$countdir = count($szint1); //measure array's length
for($i=0;$i<$countdir;$i++){
echo $szint1[$i] . "<br>"; //list directories in my given root
$szint2 = scandir($dir . "/" . $szint1[$i]); //szint2 as the new root folder
unset($szint2[array_search('.', $szint2, true)]); //remove dots
unset($szint2[array_search('..', $szint2, true)]);
$countdir2 = count($szint2); //measure 2nd array's length
for ($j=0;$j<$countdir2;$j++){
echo $szint2[$j] . "<br>"; //list directories in new root
$szint3 = scandir($dir . "/" . $szint1[$j] . "/" . $szint2[$j]); //szint2 as the new root folder (../pdoks/szint1/szint2)
unset($szint3[array_search('.', $szint3, true)]); //remove dots
unset($szint3[array_search('..', $szint3, true)]);
$countdir3 = count($szint3);
for ($k=0;$k<$countdir3;$k++){
echo $szint3[$k] . "<br>"; //list directories in new root level 3 folders
//...etc
}
}
}
错误是:
"未定义偏移量:0" //或任何数字 "scandir(../pdoks//PoosIstvan): 无法打开目录:没有这样的文件或目录”
//路径中似乎缺少 2 级
【问题讨论】:
-
搜索
recursive functions,你需要它们。递归函数是调用自身的函数。 -
哦,听起来正是需要:D
标签: php arrays list recursion scandir