You want to do something to all the files in a directory and in any subdirectories. For example, you want to see how much disk space is consumed by all the files under a directory.
Use a RecursiveDirectoryIterator and a RecursiveIteratorIterator. The RecursiveDirectoryIterator extends the DirectoryIterator with a getChildren( ) method that provides access to the elements in a subdirectory. The RecursiveIteratorIterator flattens the hierarchy that the RecursiveDirectoryIterator returns into one list. Example 24-24 counts the total size of files under a directory.
Processing all files in a directory recursively
The objects that the RecursiveDirectoryIterator spits out (and therefore that the RecursiveIteratorIterator passes along) are the same as what you get from DirectoryIterator, so all the methods mentioned in Table 24-4 are available.
24.9.4. See Also
Documentation on RecursiveDirectoryIterator at http://www.php.net/~helly/php/ext/spl/classRecursiveDirectoryIterator.html and RecursiveIteratorIterator at http://www.php.net/~helly/php/ext/spl/classRecursiveIteratorIterator.html.