duanhuan6336 2015-09-14 11:48
浏览 70
已采纳

PHP嵌套文件树

I have searched on google and on this site for something similar to what I want but nothing fits exactly and all my efforts to adapt them have failed, I want to make a script that will map out its directory and all its subfolders and the folders be at the top of the subdirectory and files after them. So far I have come up with this:

<?php
$rawParent = scandir('.');
$parentDir = array_diff($rawParent, array('.', '..','cgi-bin','error_log'));

$arrayOfDirectories = array();
$arrayOfFiles = array();

foreach ($parentDir as $child){
    if(is_dir($child)){
        array_push($arrayOfDirectories,$child);
    }else{
        array_push($arrayOfFiles,$child);
    }
}
foreach ($arrayOfDirectories as $directory){
    echo $directory.'<br>';
}
echo "<br>";
foreach ($arrayOfFiles as $file){
    echo "<a href='".$file."'>".$file.'</a><br>';
}
?>

It's good so far but it only does the first level of the directory, can this code be adapted to go through all levels of folders and nest them? If so how? I need a few pointers, I will further use javascript to have toggles on the folders to see the contents, so I will need PHP to output something nested. Sorry if I am not making much sense, don't really know how to explain.

  • 写回答

2条回答 默认 最新

  • dtoka218420 2015-09-14 11:59
    关注

    Use recursive function like this :

    <?php
    function list_directory($directory)
    { 
        $the_directory = opendir($directory) or die("Error $directory doesn't exist");
        while($file = @readdir($the_directory))
        { 
    
            if ($file == "." || $file == "..") continue;
    
    
            if(is_dir($directory.'/'.$file))
            { 
                print '<ul>'.$directory.'/'.$file;
                list_directory($directory.'/'.$file);
                print '</ul>';
             }
            else
            { 
                print "<li> $file  </li>";
             }
    
         }
    
        closedir($the_directory);
     }
    
    $path_to_search = '/var/www';
    list_directory($path_to_search);
     ?>
    

    Version with storage in array :

    <?php
    function list_directory($directory, &$storage)
    { 
        $the_directory = opendir($directory) or die("Error $directory doesn't exist");
        while($file = @readdir($the_directory))
        { 
    
            if ($file == "." || $file == "..") continue;
    
    
            if(is_dir($directory.'/'.$file))
            { 
                list_directory($directory.'/'.$file, $storage);
             }
            else
            { 
                $storage[] = $file;
             }
    
         }
    
        closedir($the_directory);
     }
    $storage = array();
    $path_to_search = '/var/www';
    list_directory($path_to_search, $storage);
    echo '<pre>', print_r($storage,true) , '</pre>';
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码