Posted By

MMDeveloper on 10/17/08


Tagged

php file recursive output directory structure listing


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

luman
vali29


recursive directory listing, plus ul/li output


 / 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.

  1. <?php
  2. print_r(makeULLI(readDirR("../")));
  3.  
  4. function readDirR($dir = "./") {
  5. $listing = opendir($dir);
  6. $return = array ();
  7. while(($entry = readdir($listing)) !== false) {
  8. if ($entry != "." && $entry != "..") {
  9. $dir = preg_replace("/^(.*)(\/)+$/", "$1", $dir);
  10. $item = $dir . "/" . $entry;
  11. if (is_file($item)) {
  12. $return[] = $entry;
  13. }
  14. elseif (is_dir($item)) {
  15. $return[$entry] = readDirR($item);
  16. } else {}
  17. } else {}
  18. }
  19.  
  20. return $return;
  21. }
  22.  
  23. function makeULLI($array) {
  24. $return = "<ol>\n";
  25.  
  26. if (is_array($array) && count($array) > 0) {
  27. foreach ($array as $k => $v) {
  28. if (is_array($v) && count($v) > 0) {
  29. $return .= "\t<li>" . $k . makeULLI($v) . "</li>\n";
  30. }
  31. else {
  32. $return .= "\t<li>" . $v . "</li>\n";
  33. }
  34. }
  35. } else {}
  36.  
  37. $return .= "</ol>";
  38.  
  39. return $return;
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46. //If you wanted to output links, this is the quickest solution I came up with (albeit dirty)
  47.  
  48. function readDirR($dir = "./") {
  49. $listing = opendir($dir);
  50. $return = array ();
  51. while(($entry = readdir($listing)) !== false) {
  52. if ($entry != "." && $entry != "..") {
  53. $dir = preg_replace("/^(.*)(\/)+$/", "$1", $dir);
  54. $item = $dir . "/" . $entry;
  55. $link = '<a href="' . $dir . "/" . $entry . '">' . $entry . '</a>';
  56. if (is_file($item)) {
  57. $return[] = $link;
  58. }
  59. elseif (is_dir($item)) {
  60. $return[$link] = readDirR($item);
  61. } else {}
  62. } else {}
  63. }
  64.  
  65. return $return;
  66. }
  67. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: redhood on February 7, 2009

How could I make the listing output link to the file/dir?

Posted By: MMDeveloper on February 9, 2009

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 {} }

return $return;

}

Posted By: MMDeveloper on February 9, 2009

ok that didn't go well... I've modified my actual code submission above

Posted By: redhood on February 9, 2009

Awesome! Thanks for the response and help mmdev.

Posted By: redhood on February 9, 2009

Awesome! Thanks for the response and help mmdev.

You need to login to post a comment.