解析PHP SPL标准库的用法(遍历目录,查找固定条件的文件)
时间:2020-04-18 14:02 来源/作者:PHP教程网
-
<?php
-
class RecursiveFileFilterIterator extends FilterIterator {
-
-
protected $ext = array('jpg','gif');
-
-
-
-
-
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;
-
}
相关文章
热门资讯