1. 程式人生 > >用PHP遍歷資料夾(遞迴)

用PHP遍歷資料夾(遞迴)

在面試的時候,我總是出了一道題,那就是利用PHP遍歷資料夾,其實這個題目考的就是大家對遞迴的認識。
    $path = '..';  
    function get_filetree($path){  
        $tree = array();  
        foreach(glob($path.'/*') as $single){  
            if(is_dir($single)){  
                $tree = array_merge($tree,get_filetree($single));  
            }  
            else{  
                $tree[] = $single;  
            }  
        }  
        return $tree;  
    }  
    print_r(get_filetree($path));