Recursive File Search (PHP)

前端 未结 8 961
逝去的感伤
逝去的感伤 2020-11-29 07:57

I\'m trying to return the files in a specified directory using a recursive search. I successfully achieved this, however I want to add a few lines of code that will allow me

8条回答
  •  天涯浪人
    2020-11-29 08:36

    You should create a filter:

    class JpegOnlyFilter extends RecursiveFilterIterator
    {
        public function __construct($iterator)
        {
            parent::__construct($iterator);
        }
    
        public function accept()
        {
            return $this->current()->isFile() && preg_match("/\.jpe?g$/ui", $this->getFilename());
        }
    
        public function __toString()
        {
            return $this->current()->getFilename();
        }
    }
    
    $it = new RecursiveDirectoryIterator("L:\folder\folder\folder");
    $it = new JpegOnlyFilter($it);
    $it = new RecursiveIteratorIterator($it);
    
    foreach ($it as $file)
        ...
    

提交回复
热议问题