gpt4 book ai didi

php - 为什么 isDot() 对我失败? (PHP)

转载 作者:可可西里 更新时间:2023-10-31 22:06:46 24 4
gpt4 key购买 nike

我正在完成一个列出目录中文件的代码段。我在目录中列出文件没有问题,但出于某种原因,我可以使用 isDot() 方法来确保文件不是“.”。要么 ”..” 。以下导致此错误:

Fatal error: Call to undefined method SplFileInfo::isDot() in ....

在我切换到使用递归迭代器之前,我使用的是目录迭代器并且它运行良好。下面的代码有什么问题吗?它应该有效。

$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 循环中调用的方法)返回一个对象,该对象本身就是 DirectoryIterator 类型。 FileSystemIterator(RecursiveDirectoryIterator 扩展)默认返回一个 SplFileInfo 的对象。您可以通过 flags 影响什么是返回

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$pathToFolder,
FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_SELF));

但在您的情况下,您无需测试项目是否为点文件。只需设置 FilesystemIterator::SKIP_DOTS,它们就不会出现。请注意,这也是 default behavior .

关于php - 为什么 isDot() 对我失败? (PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6337810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com