简体   繁体   English

RecursiveIterator 没有按预期工作

[英]RecursiveIterator not working as expected

Here is my function:这是我的 function:

protected function getContents($absoluteDirectoryPath = false, $recursive = false, $filter = "/.$/") {
    if(!$absoluteDirectoryPath) return false;
    $iterator  = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($absoluteDirectoryPath));
    $files = new RegexIterator($iterator, $filter);
    return $iterator;
}

And i call it like this:我这样称呼它:

$pageFiles = $this->getContents($pagepath);

foreach($pageFiles as $file) {
    echobr("PageFile: " . $file->fileName);
}

This is the error I get:这是我得到的错误:

Notice: Undefined property: SplFileInfo::$fileName

If I var_dump $pageFiles I get this:如果我 var_dump $pageFiles我得到这个:

object(DirectoryIterator)#5 (4) {
  ["pathName":"SplFileInfo":private]=>
  string(68) "/home/domain/example/public/home/home.php"
  ["fileName":"SplFileInfo":private]=>
  string(12) "home.php"
  ["glob":"DirectoryIterator":private]=>
  bool(false)
  ["subPathName":"RecursiveDirectoryIterator":private]=>
  string(0) ""
}

Whats going on?这是怎么回事? Why can't I loop through the results?为什么我不能遍历结果? There are 4 files in this directory and I should be able to output the filename.这个目录有4个文件,我应该可以output文件名。

The SplFileInfo class does not have a (public) filename or fileName property. SplFileInfo class fileName (公共) filename或文件名属性。 However, it has agetFilename() method.但是,它有一个getFilename()方法。 You can use it to get the filename:您可以使用它来获取文件名:

foreach($pageFiles as $file) {
    echobr("PageFile: " . $file->getFilename());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM