目录迭代器和递归目录迭代器

Obm*_*nen 0 php recursion iterator

我必须列出目录中的所有文件和文件夹:

            $images = array();
            $dirs   = array();

            $dir = new DirectoryIterator($upload_dir_real);

             foreach ($dir as $file) {
               if ($file->isDot()) {
                    continue;
                }

                if ($file->isDir()) {
                    // dir
                    $scanned_dirs[] = $file->getPath();
                      continue;
                } else {
                    // file

                    //echo $file->getFilename() . "<br>\n";//DEBUG
                    $realfile =  $file->getFilename() . "<br>\n";
                    $realpath = $file->getPathname();
                    echo realpath($realfile);//DEBUG
                    $file->getFilename();
                    $images[] = realpath( $realpath );
                }

             }
Run Code Online (Sandbox Code Playgroud)

这工作正常(没有错误),但当然只计算根,所以我尝试递归:

            $images = array();
            $dirs   = array();
$dir = new RecursiveDirectoryIterator($upload_dir_real);

             foreach ($dir as $file) {
               if ($file->isDot()) {
                    continue;
                }

                if ($file->isDir()) {
                    // dir
                    $scanned_dirs[] = $file->getsubPath();
                      continue;
                } else {
                    // file

                    //echo $file->getFilename() . "<br>\n"; //DEBUG
                    $realfile =  $file->getsubFilename() . "<br>\n";
                    $realpath = $file->getsubPathname();
                    echo realpath($realfile);//DEBUG
                    $file->getFilename();
                    $images[] = realpath( $realpath );
                }

             }
Run Code Online (Sandbox Code Playgroud)

基本上,我改变了getPath();with getsubPath()(和等价物).问题是它给我一个错误:

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

所以我搜索了一会儿发现了这个:

为什么isDot()失败了?(PHP)

这基本上是同样的问题,但是当我尝试时,我收到此错误:

Fatal error: Class 'FilesystemIterator' not found in in blah blah path

问题:

1 - 为什么另一个接受的答案中描述的方法对我不起作用?2 - 在同一个答案中,以下代码是什么:

new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator(
    $pathToFolder,
    FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_SELF));
Run Code Online (Sandbox Code Playgroud)

这实际上叫RecursiveIteratorIterator两次?我的意思是,如果它是递归的,它不能递归两次:-)

2b - 怎么FilesystemIterator没找到,即使PHP手册说明(据我的理解)它是构建递归迭代器的一部分?

(这些问题是因为我想要更好地理解,而不是仅仅复制和粘贴答案).

3 - 是否有更好的方法列出跨平台的所有文件夹和文件?

Bab*_*aba 6

  • 1 - 为什么在另一个被接受的答案中描述的方法对我不起作用?`

据我所知.代码工作正常但你的实现是错误的,你正在使用以下

   $dir = new RecursiveDirectoryIterator($upload_dir_real);
Run Code Online (Sandbox Code Playgroud)

代替

    $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($upload_dir_real));
Run Code Online (Sandbox Code Playgroud)
  • 在那个相同的答案实际上调用RecursiveIteratorIterator两次?我的意思是,如果它是递归的,它不能递归两次... :-))

不,它没有不同

RecursiveIteratorIterator != RecursiveDirectoryIterator != FilesystemIterator
            ^                             ^                    
Run Code Online (Sandbox Code Playgroud)
  • 怎么没找到FilesystemIterator,即使php手册说明(据我的理解)它是构建递归迭代器的一部分?

您已经在评论中回答了您的自我使用的PHP版本5.2.9已不再受支持或推荐

  • 3 - 有没有更好的方法列出跨平台的所有文件夹和文件?

既然你已经解决了所有需要,FilesystemIterator::SKIP_DOTS那么你就不必打电话了$file->isDot()

$fullPath = __DIR__;
$dirs = $files = array();

$directory = new RecursiveDirectoryIterator($fullPath, FilesystemIterator::SKIP_DOTS);
foreach (new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST) as $path ) {
    $path->isDir() ? $dirs[] = $path->__toString() : $files[] = realpath($path->__toString());
}

var_dump($files, $dirs);
Run Code Online (Sandbox Code Playgroud)