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 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示