如何在PHP中以递归方式返回完整路径的目录?

chu*_*tsu 3 php directory

我试图在linux或unix系统上获得类似"tree"命令的行为,其中函数返回其完整路径中的列表或目录数组.

~/
~/Pictures
~/Movies
~/Downloads
~/Documents
~/Documents/work
~/Documents/important
~/Documents/bills
~/Music
~/Music/80s/
Run Code Online (Sandbox Code Playgroud)

等......等等......

ako*_*ond 7

foreach (new RecursiveIteratorIterator (new RecursiveDirectoryIterator ('.')) as $x)
{
        echo $x->getPathname (), "\n";
}
Run Code Online (Sandbox Code Playgroud)

更新#1:

如果还要列出空目录,请使用RecursiveIteratorIterator :: CHILD_FIRST

foreach (new RecursiveIteratorIterator (new RecursiveDirectoryIterator ('.'), RecursiveIteratorIterator::CHILD_FIRST) as $x)
{
    echo $x->getPathname (), "\n";
}
Run Code Online (Sandbox Code Playgroud)