为什么isDot()失败了? (PHP) [英] Why does isDot() fail on me? (PHP)
问题描述
我正在最终确定列出目录中文件的代码段。我没有问题列出目录中的文件,但出于某种原因,我可以让isDot()方法工作,以确保文件不是。要么 .. 。以下结果导致此错误:
I'm finalizing a code segment that lists the files in a directory. I have no problems listing the files in a directory but for some reason I can get the isDot() method to work to make sure the file isn't a "." or ".." . The following below results in this error:
Fatal error: Call to undefined method SplFileInfo::isDot() in ....
在我切换到使用Recursive Iterator之前,我使用的是Directory Iterator,它工作正常。下面的代码有什么问题吗?它应该工作。
Before I switched over to using the Recursive Iterator I was using the Directory Iterator and it worked fine. Is there anything wrong with the code below? It should work.
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($pathToFolder));
//if there is a subdirectory it makes sure the proper extension is passed
foreach($files as $name => $file){
if (!$file->isDot()) { //this is where it shuts me down
$realfile = str_replace($pathToFolder, "", $file);
$url = getDownloadLink($folderID, $realfile);
$fileArray[] = $url;
}
}
推荐答案
这是因为 DirectoryIterator :: current()
(方法,即在 foreach
-loop中调用)返回一个对象,该对象本身的类型为 DirectoryIterator
。 FileSystemIterator
( RecursiveDirectoryIterator
extends)返回 SplFileInfo
的对象是默认的。您可以通过标志来影响,返回什么
This is, because DirectoryIterator::current()
(the method, that is call within a foreach
-loop) returns an object, which is itself of type DirectoryIterator
. FileSystemIterator
(that RecursiveDirectoryIterator
extends) returns an object of SplFileInfo
be default. You can influence, what is return, via flags
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$pathToFolder,
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_SELF));
但在您的情况下,如果某个项目是点文件,则无需测试。只需设置 FilesystemIterator :: SKIP_DOTS
,它们就不会出现。请注意,这也是默认行为。
But in your case, you don't need to test, if an item is a dot-file. Just set FilesystemIterator::SKIP_DOTS
and they will not appear at all. Note, that this is also the default behavior.
这篇关于为什么isDot()失败了? (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!