Using PHP SPL iterators for recursive processing of filesystem directories

Rate this item
(0 votes)

For final packaging of Joomla components using Ant build tool was necessary to implement simple PHP function allowing to check and insert when not present dummy index.html files into installation file directories.

Old approach (PHP4 like) requires to code entire recursive search for finding missing index.html under the given root path. With PHP5 SPL iterators it is possible to do it with short code as shows this test snippet which scans all subdirectories and prints subdirectory paths not containing any index.html file.

Using SPL ParentIterator applied on initial RecursiveDirectoryIterator we prepare here iteration only over root subdirectories. With RecursiveIteratorIterator it is possible to use simple iteration by means of 'foreach' command.

$path = "/projects/joomla/components/jot";

$iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME);

$dirsOnly = new ParentIterator($iterator);

$objects = new RecursiveIteratorIterator($dirsOnly, RecursiveIteratorIterator::CHILD_FIRST);

foreach ($objects as $name => $object) {

  $files = glob($name . "/index.html");

  if (count($files) === 0) {

               echo "$name\n";



Read 2962 times Last modified onMonday, 06 August 2012 15:00

Only registred users have rights to post comments. Please log-in or create an account.

We have 44 guests and no members online
Copyright © 2015 JotComponents
We have 44 guests and no members online
Copyright © 2022 JotComponents