我正在最终确定一个列出目录中文件的代码段.我没有问题列出目录中的文件,但由于某种原因,我可以让isDot()方法工作,以确保该文件不是"." 要么 ".." .以下结果导致此错误:
Fatal error: Call to undefined method SplFileInfo::isDot() in ....
Run Code Online (Sandbox Code Playgroud)
在我切换到使用Recursive Iterator之前,我正在使用Directory Iterator,它工作正常.下面的代码有什么问题吗?它应该工作.
$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;
}
}
Run Code Online (Sandbox Code Playgroud)
Kin*_*nch 31
这是因为DirectoryIterator::current()(该方法,即在foreach-loop中调用)返回一个对象,该对象本身就是类型DirectoryIterator.FileSystemIterator(RecursiveDirectoryIterator扩展)返回SplFileInfo默认对象.你可以通过旗帜影响,返回什么
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$pathToFolder,
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_SELF));
Run Code Online (Sandbox Code Playgroud)
但在您的情况下,如果项目是点文件,则无需测试.刚设置FilesystemIterator::SKIP_DOTS,它们根本不会出现.请注意,这也是默认行为.