当前位置:首页 > PHP教程 > PHP高级教程

解析PHP SPL标准库的用法(遍历目录,查找固定条件的文件)

<?php
 class recursivefilefilteriterator extends filteriterator {
     // 满足条件的扩展名
     protected $ext = array('jpg','gif');

     /**
      * 提供 $path 并生成对应的目录迭代器
      */
     public function __construct($path) {
         parent::__construct(new recursiveiteratoriterator(new recursivedirectoryiterator($path)));
     }

     /**
      * 检查文件扩展名是否满足条件
      */
     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;
 }


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!