Skip to content

Instantly share code, notes, and snippets.

@glagola
Created August 24, 2012 09:07
Show Gist options
  • Save glagola/3447864 to your computer and use it in GitHub Desktop.
Save glagola/3447864 to your computer and use it in GitHub Desktop.
List Files deeply
<?php
function files($dir, $callback) {
$queue = array($dir);
$r = -1;
$w = 0;
while ($r < $w) {
$path = $queue[++$r];
$dir = dir($path);
while (FALSE !== ($entry = $dir->read())) {
if (FALSE !== strstr('..', $entry)) continue;
$entry = $path . DIRECTORY_SEPARATOR . $entry;
if (is_dir($entry)) {
if (is_executable($entry)) {
$queue []= $entry;
$w++;
}
} else {
$callback($entry);
}
}
$dir->close();
}
}
files('/etc', function ($file) {
echo $file, PHP_EOL;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment