简体   繁体   English

如何使用RecursiveIteratorIterator忽略目录?

[英]How to ignore directories using RecursiveIteratorIterator?

I have tried several methods to ignore certain directories using RecursiveIteratorIterator on a file system. 我已经尝试了几种方法来在文件系统上使用RecursiveIteratorIterator忽略某些目录。

For the sake of an example say I want to ignore the following directory: /cache . 为了举例说明我想忽略以下目录: /cache

My Iterator looks like this: 我的Iterator看起来像这样:

//$dirname is root
$directory = new RecursiveDirectoryIterator($dirname);
$mega = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);

foreach ($mega as $fileinfo) {
 echo $fileinfo;
}

I have been able to ignore certain file extensions using pathinfo for example this works: 我已经能够使用pathinfo忽略某些文件扩展名,例如这有效:

$filetypes = array("jpg", "png");
$filetype = pathinfo($fileinfo, PATHINFO_EXTENSION);

foreach ($mega as $fileinfo) {
    if (!in_array(strtolower($filetype), $filetypes)) {
    echo $fileinfo;
    }
}

When I try it using PATHINFO_DIRNAME (with the appropriate directory paths in the array) it does not work, there are no errors, it just does not ignore the directories. 当我使用PATHINFO_DIRNAME (在数组中使用相应的目录路径)尝试它时,它不起作用,没有错误,它只是不忽略目录。

I have also experimented with using FilterIterator to no avail, and now I'm thinking maybe I should use RegexIterator . 我也试过使用FilterIterator无济于事,现在我想也许我应该使用RegexIterator

What would be the simplest and most efficient way to ignore directories using RecursiveIteratorIterator ? 使用RecursiveIteratorIterator忽略目录的最简单,最有效的方法是什么?


Experiments that didn't work. 实验没有用。 Using PATHINFO_DIRNAME 使用PATHINFO_DIRNAME

$dirignore = array("cache", "cache2");
$directory = new RecursiveDirectoryIterator($dirname);
$mega = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
$dirtype = pathinfo($fileinfo, PATHINFO_DIRNAME); 

if (!in_array($dirtype), $dirignore)) {
echo $fileinfo;  //no worky still echo directories
}

Using FilterIterator (not sure how this works) 使用FilterIterator (不知道这是如何工作的)

class DirFilter extends FilterIterator
{
    public function accept()
    {
        //return parent::current() != "..\cache\";

       $file = $this->getInnerIterator()->current();
       if ($file != "..\cache")
       return $file->getFilename();

      //also tried with same error as below
      //return !preg_match('/\cache', $file->getFilename());
    }
}

$directory= new RecursiveDirectoryIterator($dirname);
$directory = new DirFilter($directory); 
$mega = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
// loop

Tis results in Error: An instance of RecursiveIterator or IteratorAggregate creating it is required Tis导致Error: An instance of RecursiveIterator or IteratorAggregate creating it is required

Your DirFilter wants to extend RecursiveFilterIterator , since it is being used with a recursive iterator. 您的DirFilter想要扩展RecursiveFilterIterator ,因为它与递归迭代器一起使用。 Not doing that, is the cause of your error. 不这样做是导致错误的原因。

class DirFilter extends RecursiveFilterIterator
{
    // …
}

The next step is figuring out how to use accept() properly. 下一步是弄清楚如何正确使用accept() It is supposed to return a true or false value, which dictates whether the current item should be included ( true ) or excluded ( false ) from the iteration. 它应该返回一个truefalse值,它指示当前项应该包含( true )还是从迭代中排除( false )。 Returning a file name from accept() is not what you want to be doing. accept()返回文件名不是您想要做的。

class DirFilter extends RecursiveFilterIterator
{
    public function accept()
    {
        $excludes = array("cache", "cache2");
        return !($this->isDir() && in_array($this->getFilename(), $excludes));
    }
}

It might be nice to be able to pass in the array of excluded directories. 能够传入排除目录数组可能会很好。 This requires a little more code but is much more reusable. 这需要更多的代码,但更可重用。

class DirFilter extends RecursiveFilterIterator
{
    protected $exclude;
    public function __construct($iterator, array $exclude)
    {
        parent::__construct($iterator);
        $this->exclude = $exclude;
    }
    public function accept()
    {
        return !($this->isDir() && in_array($this->getFilename(), $this->exclude));
    }
    public function getChildren()
    {
        return new DirFilter($this->getInnerIterator()->getChildren(), $this->exclude);
    }
}

This code could then be used like: 然后可以使用此代码,如:

$directory = new RecursiveDirectoryIterator($dirname);
$filtered = new DirFilter($directory, $dirignore); 
$mega = new RecursiveIteratorIterator($filtered, …);

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

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