2011-05-16 47 views
3

我試圖在linux或unix系統上得到類似「樹」命令的行爲,其中函數返回完整路徑中的目錄列表或數組。如何在PHP中遞歸地返回完整路徑的目錄?

~/ 
~/Pictures 
~/Movies 
~/Downloads 
~/Documents 
~/Documents/work 
~/Documents/important 
~/Documents/bills 
~/Music 
~/Music/80s/ 

等....等...

回答

7
foreach (new RecursiveIteratorIterator (new RecursiveDirectoryIterator ('.')) as $x) 
{ 
     echo $x->getPathname(), "\n"; 
} 

更新#1:

如果你想空目錄被列爲好,使用RecursiveIteratorIterator :: CHILD_FIRST

foreach (new RecursiveIteratorIterator (new RecursiveDirectoryIterator ('.'), RecursiveIteratorIterator::CHILD_FIRST) as $x) 
{ 
    echo $x->getPathname(), "\n"; 
} 
+0

使用此功能的問題是,它不返回空目錄。也是必要的getPathname()?我試過沒有,它似乎很好地工作?再次感謝! :) – chutsu 2011-05-16 14:45:37

+0

看到我的更新。當然,不需要使用getPathname()。 – akond 2011-05-16 17:09:43

+0

這產生點文件奇怪,因爲我認爲標誌SKIP_DOTS是RecursiveDirectoryIterator的默認值? – Titan 2015-05-01 13:44:05

相關問題