Home >Backend Development >PHP Tutorial >php uses spl library to traverse files

php uses spl library to traverse files

WBOY
WBOYOriginal
2016-07-29 09:15:341023browse

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(&#39;./&#39;, &#39;php&#39;);
	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.

Statement:
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