我试图在linux或unix系统上获得类似“tree”命令的行为,其中函数返回其完整路径中的列表或目录数组。
示例
~/
~/Pictures
~/Movies
~/Downloads
~/Documents
~/Documents/work
~/Documents/important
~/Documents/bills
~/Music
~/Music/80s/
等等......等等。
答案 0 :(得分: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";
}
答案 1 :(得分:1)
结帐PHP的Recursivedirectoryiterator。它会做你需要的,它有一些很好的例子。