Parse the usage of PHP SPL standard library (traverse directories and find files with fixed conditions)_PHP Tutorial

WBOY
Release: 2016-07-21 15:05:19
Original
873 people have browsed it

 class RecursiveFileFilterIterator extends FilterIterator {
     // 满足条件的扩展名
     protected $ext = array('jpg','gif');

     /**
* Provide $path and generate the corresponding directory iterator
*/
     public function __construct($path) {
         parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
     }

     /**
* Check whether the file extension meets the conditions
*/
     public function accept() {
         $item = $this->getInnerIterator();
         if ($item->isFile() && 
                 in_array(pathinfo($item->getFilename(), PATHINFO_EXTENSION), $this->ext)) {
             return TRUE;
         }
     }
 }

 // 实例化
 foreach (new RecursiveFileFilterIterator('D:/history') as $item) {
     echo $item . PHP_EOL;
 }

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327702.htmlTechArticle?php class RecursiveFileFilterIterator extends FilterIterator { // 满足条件的扩展名 protected $ext = array('jpg','gif'); /** * 提供 $path 并生成对应的目录迭代器...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template