日期:2014-05-17  浏览次数:20631 次

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>';
           }

           
        }
   
    }

?>