/ Published in: PHP
This will accept a directory reference, and take a recursive listing of files and directories. The 2nd function will then create a structured ul/li list of the results. The functions can be used in combination as illustrated, or with just the recursive directory lister alone.
Expand |
Embed | Plain Text
<?php function readDirR($dir = "./") { if ($entry != "." && $entry != "..") { $item = $dir . "/" . $entry; $return[] = $entry; } $return[$entry] = readDirR($item); } else {} } else {} } return $return; } function makeULLI($array) { $return = "<ol>\n"; foreach ($array as $k => $v) { $return .= "\t<li>" . $k . makeULLI($v) . "</li>\n"; } else { $return .= "\t<li>" . $v . "</li>\n"; } } } else {} $return .= "</ol>"; return $return; } //If you wanted to output links, this is the quickest solution I came up with (albeit dirty) function readDirR($dir = "./") { if ($entry != "." && $entry != "..") { $item = $dir . "/" . $entry; $link = '<a href="' . $dir . "/" . $entry . '">' . $entry . '</a>'; $return[] = $link; } $return[$link] = readDirR($item); } else {} } else {} } return $return; } ?>
Comments
Subscribe to comments
You need to login to post a comment.

How could I make the listing output link to the file/dir?
quickest way would be to just modify the readDirR function as follows
function readDirR($dir = "./") { $listing = opendir($dir); $return = array (); while(($entry = readdir($listing)) !== false) { if ($entry != "." && $entry != "..") { $dir = pregreplace("/^(.*)(\/)+$/", "$1", $dir); $item = $dir . "/" . $entry; if (isfile($item)) { $return[] = '<a href="'>' . $entry . ''; } elseif (is_dir($item)) { $return['<a href="'>' . $entry . ''] = readDirR($item); } else {} } else {} }
}
ok that didn't go well... I've modified my actual code submission above
Awesome! Thanks for the response and help mmdev.
Awesome! Thanks for the response and help mmdev.