普通网友 2019-08-15 08:43
浏览 83

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

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

  • 写回答

1条回答 默认 最新

  • doujiongqin0687 2019-08-15 08:55
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧