Home  >  Article  >  Backend Development  >  php 深度优先递归输出路径上所有文件

php 深度优先递归输出路径上所有文件

WBOY
WBOYOriginal
2016-06-13 13:25:09893browse

php 深度优先递归输出路径下所有文件

<?php 

    $dir = "/home/mengjun/IdeaProjects/phone/original-data/data/jiangsu";
    fun($dir,0);
    function fun($dir,$dep){
        $floders  = array();
        $files = array();
        if (is_dir($dir)) {
           $temp = scandir($dir,$dep);
           foreach($temp as $t){
                if(is_dir($dir.'/'.$t)){
                    if($t[0]!='.'){
                        $floders[] = $dir.'/'.$t; 
                    }
                }else{
                    $files[] = $dir.'/'.$t;
                }
           }
          
           $tab = '';
           for($i = 0;$i<$dep;$i++){
                    $tab .= '&nbsp&nbsp&nbsp&nbsp';
           }
           foreach($floders as $f){
                echo $tab.'floder: '.$f.'<br>';
                fun($f,++$dep);
           }
           foreach($files as $f){
                echo $tab.'file: '.$f.'<br>';
           }

           
        }
   
    }

?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn