Recipe 24.8. Getting a List of Filenames Matching a Pattern


24.8.1. Problem

You want to find all filenames that match a pattern.

24.8.2. Solution

Use a FilterIterator subclass with DirectoryIterator. The FilterIterator subclass needs its own accept( ) method that decides whether or not a particular value is acceptable. The code in Example 24-22 only accepts filenames that end with common extensions for images.

Using a FilterIterator

<?php class ImageFilter extends FilterIterator {     public function accept() {         return preg_match('@\.(gif|jpe?g|png)$@i',$this->current());     } } foreach (new ImageFilter(new DirectoryIterator('/usr/local/images')) as $img) {     print "<img src='/books/3/131/1/html/2/".htmlentities($img)."'/>\n"; } ?>

24.8.3. Discussion

The FilterIterator encloses a DirectoryIterator and only allows certain elements to emerge. It's up to the accept( ) method to return true or false to indicate whether a particular element (accessed with $this->current( )) is OK. In Example 24-22, accept( ) uses a regular expression to make that determination, but your code can use any logic you like.

If your pattern can be expressed as a simple shell "glob" (e.g. *.*), use the glob( ) function to get the matching filenames. Example 24-23 finds all the text files in a particular directory.

Using glob( )

<?php foreach (glob('/usr/local/docs/*.txt') as $file) {    $contents = file_get_contents($file);    print "$file contains $contents\n"; } ?>

The glob( ) function returns an array of matching full pathnames. If no files match the pattern, glob( ) returns false.

24.8.4. See Also

Recipe 24.9 details iterating through each file in a directory recursively; documentation on FilterIterator at http://www.php.net/~helly/php/ext/spl/classFilterIterator.html and on glob( ) at http://www.php.net/glob; information about shell pattern matching is available at http://www.gnu.org/software/bash/manual/bashref.html#SEC35.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net