当前位置: 移动技术网 > IT编程>开发语言>PHP > 解析PHP SPL标准库的用法(遍历目录,查找固定条件的文件)

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

2019年05月26日  | 移动技术网IT编程  | 我要评论
<?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;
 }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网