Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Created April 29, 2015 04:44
Show Gist options
  • Select an option

  • Save TechplexEngineer/9feb3e26e75e9dce6c6e to your computer and use it in GitHub Desktop.

Select an option

Save TechplexEngineer/9feb3e26e75e9dce6c6e to your computer and use it in GitHub Desktop.
Recursive print directory tree
<?php
function scandir_folders($d, $blacklist = array('.', '..')) {
return array_filter(scandir($d), function ($f) use($d, $blacklist) {
return is_dir($d . DIRECTORY_SEPARATOR . $f) && !in_array($f, $blacklist);
});
}
function scandir_all($d, $blacklist = array('.', '..')) {
return array_filter(scandir($d), function ($f) use($d, $blacklist) {
return !in_array($f, $blacklist);
});
}
//@todo probably should limit the depth
function recurse_scandir($d)
{
foreach (scandir_all($d) as $item)
{
$path = $d.'/'.$item;
if (is_dir($path))
{
echo "$path";
echo "<ul>";
recurse_scandir($path);
echo "</ul>";
}
else
{
echo "<li>";
echo '<a href="'.$path.'/'.$item.'">'.$item.'</a>';
echo "</li>";
}
}
}
echo "<h1>Directory Tree</h1>";
recurse_scandir('./src')
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment