RecursiveIterator迭代器空白页
我正在尝试使用 RecursiveIteratorIterator
和 RecursiveDirectoryIterator
。
我想获取 c:\
文件夹中的所有文件。但我不知道为什么我无法得到结果,而是一个空白页。
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('c:/' ));
foreach( $it as $file ) {
$all[] = $file->getRealPath();
}
print_r($all);
但如果我使用这段代码,它就可以工作
foreach( $it as $key=>$file )
{
echo $key."=>".$file."\n";
}
I'm trying to use RecursiveIteratorIterator
and RecursiveDirectoryIterator
.
I want get all file inside my c:\
folder. But i don't know why i can't get the result but a blank page.
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('c:/' ));
foreach( $it as $file ) {
$all[] = $file->getRealPath();
}
print_r($all);
but if i use this code, it's work
foreach( $it as $key=>$file )
{
echo $key."=>".$file."\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很可能是因为您的 PHP 解释器无权访问该文件夹。
Most likely because your PHP interpreter does not have access to that folder.
这是因为 PHP 的
StdObj
类不能用作数组,因为它是关联数组。这句话用词是错误的,但我无法更好地描述它。 PHP 对象被强制转换或类似的东西。如果
$data
是一个对象,那么如果您想将$value
作为对象访问,则这是必需的。编辑:顺便说一句,这是一个很好的问题,我自己花了几个小时来解决这个问题。
It's because PHPs
StdObj
-class cannot be used as an array because it's an associative array. This is wrong by the words but I cannot describe it better. The PHP object gets casted or something like that.If
$data
is an object then this is required if you want to access$value
as an object.Edit: This is a good question btw, I've spent a couple of hours myself figuring this out.