列出目录和子目录并将每个级别存储在数组中

Want to scandir the root folder first, store the found directories in array (level 1). Then scandir each sub-directory that I've found and store them inside level 1, as level 2 and so on.

How can I do it with max 5 levels, and display on the screen as a folder tree.

I've made a terrible code with a lot of error, but half-working:

    $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
            }
        }

    }

Errors are:

"Undefined offset: 0" //or any number " scandir(../pdoks//PoosIstvan): failed to open dir: No such file or directory"

//seems level 2 missing from the path

donglian1523
donglian1523 哦,听起来恰好需要:D
大约一年之前 回复
dounei9043
dounei9043 搜索递归函数,您需要它们。递归函数是调用自身的函数。
大约一年之前 回复

1个回答



网上有很多例子可以获得递归的文件和文件夹列表。 你应该避免嵌套循环,尝试使用recusrive函数调用或迭代方式
使用PHP递归函数列出目录中的所有文件和文件夹 </ p>
</ div>

展开原文

原文

There are plenty of examples on the web to get a recursive list of files and folders. You should avoid nesting loops, try to use a recusrive function call or an iterative way List all the files and folders in a Directory with PHP recursive function

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐