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条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)