【发布时间】:2013-01-02 20:53:13
【问题描述】:
我想获取所有子目录的列表,并且我的以下代码可以正常工作,除非我对某些文件夹具有只读权限。
在下面的问题中,它显示了如何使用 RecursiveDirectoryIterator 跳过目录 Can I make RecursiveDirectoryIterator skip unreadable directories? 但是我的代码在这里略有不同,我无法解决这个问题。
$path = 'www/';
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path,RecursiveDirectoryIterator::KEY_AS_PATHNAME),
RecursiveIteratorIterator::CHILD_FIRST) as $file => $info)
{
if ($info->isDir())
{
echo $file . '<br>';
}
}
我得到了错误
Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(../../www/special): failed to open dir: Permission denied'
我已尝试将其替换为另一个问题中已接受的答案。
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator("."),
RecursiveIteratorIterator::LEAVES_ONLY,
RecursiveIteratorIterator::CATCH_GET_CHILD);
但是,这段代码不会像我想要的那样给我一个 www 内所有目录的列表,我在哪里出错了?
【问题讨论】:
-
如果不返回www里面的所有目录,返回什么?
-
它返回目录,直到它得到一个它没有权限读取的目录,我收到错误,我希望能够跳过那些受限文件夹并返回目录中的所有其他文件夹
标签: php directory directory-walk