Wednesday, 13 Feb 2008 7:02PM EST
PHP Recursive Directory Traversal
A recursive function, simply put, is a function that calls itself. Certain functional languages, such as LISP and Scheme, are designed with recursion in mind; and recursive functions are often required to complete a program. PHP, on the other hand, focuses on iteration. Recursion is often very costly in PHP and not usually your best option. In the case of traversing a file system, though, recursion is the best and most efficient option.The basic idea behind this code is that you have a single function which scans a given directory and stores the name of each file in an array. The function will call itself each time a sub directory is found, passing the name of that sub directory to the function. This will put the first function call on hold while the second function call is evaluated. The second function call will scan the sub directory, putting each filename into a sub array. The function will call itself again if a third level directory is found, and so on. This will continue on until all directories have been scanned. Assuming the following directory structure:
root/html root/html/index.php root/html/about.php root/html/archives.php root/html/images/logo.png root/html/images/photo.png root/html/images/icons/arrow.png root/html/images/icons/pencil.png root/html/images/icons/paper.png root/html/style/style.css
$dir = "root/html"; $dirStructure = scanFilesystem($dir); print_r($dirStructure);
Array
(
[0] => about.php
[1] => archives.php
[2] => index.php
[images] => Array
(
[0] => logo.png
[1] => photo.png
[icons] => Array
(
[0] => arrow.png
[1] => pencil.png
[2] => paper.png
)
)
[style] => Array
(
[0] => style.css
)
)
We call scanFilesystem() again, from within the function itself, passing it the path of the sub directory that we've found and storing the result in $tempArray[$item]; where $item is the name of the sub directory. This process will continue until a directory with no sub directories is reached. In that case, the function goes back to the next higher directory and continues that scan, and so on until every directory has been scanned. After each directory scan has finished, we sort the files and sub directories in the resulting array with asort(). Each function call returns $tempArray[], which is now a sorted array of all files and directories (including those contained in sub directories) within a particular directory.
function scanFilesystem($dir) {
$itemArray = scandir($dir);
for($i=2; $i<count($itemArray); $i++) {
$item = $itemArray[$i];
$newDir = $dir . "/" . $item;
if(is_dir($newDir)) {
$tempArray[$item] = scanFilesystem($newDir);
asort($tempArray);
}
else {
$tempArray[] = $item;
}
}
return $tempArray;
}
As always, if you have a technique that you think is better or a tweak that improves on this code, please share it in the comments. If you have a tip, trick or an idea for my next post, please don't hesitate to contact me.
















1 Comments So Far