Maison  >  Article  >  développement back-end  >  关于目录遍历,结果为树形结构的有关问题,请上

关于目录遍历,结果为树形结构的有关问题,请上

WBOY
WBOYoriginal
2016-06-13 13:14:39780parcourir

关于目录遍历,结果为树形结构的问题,请高手指点下
我想递归遍历一个目录,然后让结果呈树形结构展示出来,请高手指点下。下面是遍历函数

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php echo "<pre class="brush:php;toolbar:false">";

$path = "E:/ProgramBack";

function ERGFile($path){
    
    
    $AllFiles = scandir($path);

    foreach($AllFiles as $key=>$file){
        
        if($file == "." || $file == ".."){
            
            continue;
            
        }
        
        if(is_dir($path."/".$file)){
            echo $path."/".$file." -- DIR <br>";
            ERGFile($path."/".$file);
        }else{
            echo $path."/".$file."<br>";
        }
    }    
    

}

ERGFile($path);

?>



如何让他以树形结构显示呢?
a -- dir
 a.tx
 a1.exe
 a1 -- dir
  aa.txt
  bb.exe
b --dir
 b2 -- dir
 b3 -- dir
  b3b.txt
  b4g.exe
..........

------解决方案--------------------
在 echo 后面插入 str_repeat(' ', substr_count($path, '/')) .
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn