Example code:
<?php class ExtentionFinder extends FilterIterator { public $predicate, $path; public function __construct($path, $predicate) { $this->predicate = $predicate; $this->path = $path; $it = new RecursiveDirectoryIterator($path); $flatIterator = new RecursiveIteratorIterator($it); parent::__construct($flatIterator); } public function accept() { $pathInfo = pathinfo($this->current()); $extension = $pathInfo['extension']; return ($extension == $this->predicate); } } ?> <?php $it = new ExtentionFinder('./', 'php'); foreach($it as $value) { echo $value."<br/>"; } ?>
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces how PHP uses the spl library to traverse files, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.