Skip to content

Instantly share code, notes, and snippets.

@rugbyprof
Created February 20, 2012 21:14
Show Gist options
  • Save rugbyprof/1871465 to your computer and use it in GitHub Desktop.
Save rugbyprof/1871465 to your computer and use it in GitHub Desktop.
Php Iterative Directory Traversal
<?php
//From php.net comments written by:
//donovan dot pp at gmail dot com
function dir_tree($dir) {
$path = '';
$stack[] = $dir;
while ($stack) {
$thisdir = array_pop($stack);
if ($dircont = scandir($thisdir)) {
$i=0;
while (isset($dircont[$i])) {
if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
$current_file = "{$thisdir}/{$dircont[$i]}";
if (is_file($current_file)) {
$path[] = "{$thisdir}/{$dircont[$i]}";
} elseif (is_dir($current_file)) {
$path[] = "{$thisdir}/{$dircont[$i]}";
$stack[] = $current_file;
}
}
$i++;
}
}
}
return $path;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment