Listing the contents of a directory with PHP

With this handy snippet you can easily display an up-to-date listing of the contents of a directory on your server. Just include this code on the index page (or any page) of a directory to see its contents.

  1. // create directory listing array
  2. $listing = array();
  3.  
  4. // open directory (NOTE: you can change opendir()'s argument to be any dir)
  5. $dh = opendir(".") or die("Couldn't open directory");
  6.  
  7. // read through dir contents while a file exists
  8. while (!(($file=readdir($dh))===false)) {
  9.  
  10. // skip special cases (NOTE: replace "index.php" with the name of the file you put this snippet in)
  11. if ($file=="index.php" || $file==".htaccess" || is_dir($file) || filesize($file)==0 || !strpos($file,".")) continue;
  12.  
  13. // produce human-readable file size
  14. $mb = null;
  15. $kb = filesize($file)/1024;
  16. if ($kb>=1000) $mb = sprintf("%.2f",$kb/1024);
  17. else $kb = sprintf("%.2f",$kb);
  18. $size = (isset($mb))? "$mb MB" : "$kb KB";
  19.  
  20. // add a link to the file to the listing array
  21. array_push($listing,"<a href=\"".rawurlencode($file)."\" title=\"Added on ".date("j M Y",filemtime($file))."\">$file</a> [$size]");
  22. }
  23.  
  24. // close directory
  25. closedir($dh);
  26.  
  27. // clear the cache
  28. clearstatcache();
  29.  
  30. // sort the files
  31. natcasesort($listing);
  32.  
  33. // echo the files in an unordered list
  34. echo "<ul>\n";
  35. foreach ($listing as $val) echo "\t<li>$val</li>\n";
  36. echo "</ul>\n";
  37. source file: dir-listing.txt

An example

My public file storage page, files.zakness, uses this snippet to display the contents of the upload directory. The code is slightly modified so I could use CSS to style and structure it. Here's the modified PHP and CSS code:

Modified PHP:

  1. $link = array();
  2. $dh = opendir(".") or die("Couldn't open directory");
  3. while (!(($file=readdir($dh))===false)) {
  4. $mb = null;
  5. if ($file=="index.php" || $file==".htaccess" || is_dir($file) || filesize($file)==0 || !strpos($file,".")) continue;
  6. $kb = filesize($file)/1024;
  7. if ($kb>=1000) $mb = sprintf("%.2f",$kb/1024);
  8. else $kb = sprintf("%.2f",$kb);
  9. $size = (isset($mb))? "$mb MB" : "$kb KB";
  10. $link[] = "<a href=\"".rawurlencode($file)."\" title=\"Added on ".date("j M Y",filemtime($file))."\">$file</a> $note<span>[$size]</span>";
  11. }
  12. closedir($dh);
  13. clearstatcache();
  14. natcasesort($link);
  15. $i = 0;
  16. echo "<ul id=\"content\">\n";
  17. foreach ($link as $val) {
  18. $class = (!($i%2))? "" : " class=\"r2\"";
  19. echo "\t<li$class>$val</li>\n";
  20. ++$i;
  21. }
  22. echo "</ul>\n";
  23. source file: dir-listing-files.txt

CSS:

  1. #content {
  2. list-style: none;
  3. padding: 0;
  4. margin: 1em;
  5. }
  6. #content li {
  7. padding: 0.2em;
  8. position: relative;
  9. line-height: 1.5em;
  10. }
  11. #content li.r2 {
  12. background-color: #f2fafe;
  13. }
  14. #content a, #content a:link, #content a:visited {
  15. font-weight: bold;
  16. text-decoration: none;
  17. color: #ff987c;
  18. }
  19. #content a:hover, #content a:active {
  20. color: #b7afa3;
  21. text-decoration: underline;
  22. }
  23. #content span {
  24. position: absolute;
  25. right: 2px;
  26. top: 0.5em;
  27. font-size: 0.8em;
  28. color: #a2b7f8;
  29. } * html #content span {right: 30px;}
  30. source file: dir-listing-css.txt

The modifications allow for assigning alternate rows different background colors, and for aligning the file sizes on the right edge of the list container (by applying css rules to the new <span> tag surrounding the file size).