dongqiu3709 2015-10-17 16:12
浏览 106
已采纳

如何在php中列出目录,子目录和所有文件

I want to be able to list all the directories, subdirectories and files in the "./" folder ie the project folder called fileSystem which contains this php file scanDir.php.

You can view the directory system I've got here: enter image description here

At the minute it will only return the subdirectory folders/files in the root of the mkdir directory but not any folders inside that subdirectory.

How do I modify the code so that it demonstrates all the files, directories, subdirectories and their files and subdirectories within the fileSystem folder given that the php file being run is called scanDir.php and the code for that is provided below. Here is the php code:

 $path = "./";

 if(is_dir($path))

{
    $dir_handle = opendir($path);

    //extra check to see if it's a directory handle.
    //loop round one directory and read all it's content.
    //readdir takes optional parameter of directory handle.
    //if you only scan one single directory then no need to passs in argument.
    //if you are then going to scan into sub-directories the argument needs 
    //to be passed into readdir.
    while (($dir = readdir($dir_handle))!== false) 
    {
    if(is_dir($dir))
    {
    echo "is dir: " . $dir . "<br>"; 


    if($dir == "mkdir") 
        {
        $sub_dir_handle = opendir($dir);
        while(($sub_dir = readdir($sub_dir_handle))!== false)
            {
            echo "--> --> contents=$sub_dir <br>";
            }
    }



    }    
        elseif(is_file($dir)) 
         {
            echo "is file: " . $dir . "<br>"  ;
        }
    }
closedir($dir_handle); //will close the automatically open dir.
}

else {

    echo "is not a directory";
}
  • 写回答

2条回答 默认 最新

  • dongliuzi3410 2015-10-17 16:42
    关注

    Use scandir to see all stuff in the directory and is_file to check if the item is file or next directory, if it is directory, repeat the same thing over and over.

    So, this is completely new code.

    function listIt($path) {
    $items = scandir($path);
    
    foreach($items as $item) {
    
        // Ignore the . and .. folders
        if($item != "." AND $item != "..") {
            if (is_file($path . $item)) {
                // this is the file
                echo "-> " . $item . "<br>";
            } else {
                // this is the directory
    
                // do the list it again!
                echo "---> " . $item;
                echo "<div style='padding-left: 10px'>";
                listIt($path . $item . "/");
                echo "</div>";
            }
        }
      }
    }
    
    echo "<div style='padding-left: 10px'>";
    listIt("/");
    echo "</div>";
    

    You can see the live demo here in my webserver, btw, I will keep this link just for a second

    When you see the "->" it's an file and "-->" is a directory

    The pure code with no HTML:

    function listIt($path) {
    $items = scandir($path);
    
    foreach($items as $item) {
        // Ignore the . and .. folders
        if($item != "." AND $item != "..") {
            if (is_file($path . $item)) {
                // this is the file
                // Code for file
            } else {
                // this is the directory
                // do the list it again!
                // Code for directory
                listIt($path . $item . "/");
            }
        }
      }
    }
    
    listIt("/");
    

    the demo can take a while to load, it's a lot of items.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 搭建kubernetes集群并部署kubesphere,以docker compose方式部署微服务项目
  • ¥15 buck、boost、buck-boost拓扑的PWM控制芯片调研
  • ¥15 Swin UNETR在自己数据集上训练时,输出的target的形状不匹配
  • ¥15 远程调试启动项目,开发工具:idea+微信开发者工具
  • ¥15 热重分析MATLAB
  • ¥15 智能驾驶算法通用测试场景
  • ¥20 Java《森林环保大冒险》游戏设计
  • ¥15 升压斩波的问题.谁能帮个忙
  • ¥15 ccs3.3连接烧录器成功,但是点烧录报错,请各位指点迷津
  • ¥15 求帮助分析DBG文件,找出系统蓝屏原因
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部