如何循序渐进地返回PHP中的完整路径? [英] How to walk a directory recursively returning the full path in PHP?

查看:155
本文介绍了如何循序渐进地返回PHP中的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试在linux或unix系统上获取类似tree命令的行为,其中函数返回一个完整路径中的目录列表或数组。 p>示例

 〜/ 
〜/图片
〜/电影
〜/下载
〜/文件
〜/文件/工作
〜/文件/重要
〜/文件/票据
〜/音乐
〜/音乐/ 80年代/

etc ....等...

解决方案

  foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator('。'))as $ x)
{
echo $ x-> getPathname(),\\\
;
}

更新#1:



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

  foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator('。'),RecursiveIteratorIterator :: CHILD_FIRST)as $ x)
{
echo $ x-> getPathname(),\\\
;
}


I'm trying to get the behavior like the "tree" command on linux or unix systems where the function returns a list or array of directories in its full path.

Example

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

etc .... etc...

解决方案

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

Update #1:

If you want empty directories to be listed as well, use RecursiveIteratorIterator::CHILD_FIRST

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

这篇关于如何循序渐进地返回PHP中的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆