Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^168) CHAPTER 11 ■ SPL FILE AND DIRECTORY HANDLING
__construct’s flags parameter controls how the current and key values are returned. To
visualize the operation of this iterator, you can use the RecursiveTreeIterator, as explained in
the previous chapter. Listing 11-6 shows an example of a directory structure.
Listing 11-6. Using RecursiveDirectoryIterator
require_once('/path/to/php-src/ext/spl/examples/recursivetreeiterator.inc');
$pathName = '/path/to/php-src/ext/spl/examples';
$iterator = new RecursiveDirectoryIterator($pathName);
$treeIterator = new RecursiveTreeIterator($iterator);
foreach($treeIterator as $entry) {
echo $entry. "\n";
}
|-/.../examples/tree.php
|-/.../examples/searchiterator.inc
|-/.../examples/tests
| |-/.../examples/tests/dualiterator_001.phpt
| -/.../examples/tests/examples.inc
|-/.../examples/directorygraphiterator.inc
|-/.../examples/dbareader.inc
|-/.../examples/directoryfilterdots.inc
-/.../examples/keyfilter.inc


Finding Files


So now that you’ve seen how to list files and directories, let’s look at how to find files using
FindFile and RegexFindFile.

FindFile
To locate files by file name, use the FindFile example iterator, as demonstrated in Listing 11-7.

Listing 11-7. Searching for a File with FindFile

require_once('/path/to/php-src/ext/spl/examples/findfile.inc');

$it = new FindFile('/path/to/php-src/', 'tree.php');

foreach($it as $entry) {
echo $entry. "\n";
}

McArthur_819-9C11.fm Page 168 Thursday, February 28, 2008 7:49 AM

Free download pdf