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.
- // create directory listing array
- $listing = array();
- // open directory (NOTE: you can change opendir()'s argument to be any dir)
- $dh = opendir(".") or die("Couldn't open directory");
- // read through dir contents while a file exists
- while (!(($file=readdir($dh))===false)) {
- // skip special cases (NOTE: replace "index.php" with the name of the file you put this snippet in)
- if ($file=="index.php" || $file==".htaccess" || is_dir($file) || filesize($file)==0 || !strpos($file,".")) continue;
- // produce human-readable file size
- $mb = null;
- $kb = filesize($file)/1024;
- if ($kb>=1000) $mb = sprintf("%.2f",$kb/1024);
- else $kb = sprintf("%.2f",$kb);
- $size = (isset($mb))? "$mb MB" : "$kb KB";
- // add a link to the file to the listing array
- array_push($listing,"<a href=\"".rawurlencode($file)."\" title=\"Added on ".date("j M Y",filemtime($file))."\">$file</a> [$size]");
- }
- // close directory
- closedir($dh);
- // clear the cache
- clearstatcache();
- // sort the files
- natcasesort($listing);
- // echo the files in an unordered list
- echo "<ul>\n";
- foreach ($listing as $val) echo "\t<li>$val</li>\n";
- echo "</ul>\n";
- 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:
- $link = array();
- $dh = opendir(".") or die("Couldn't open directory");
- while (!(($file=readdir($dh))===false)) {
- $mb = null;
- if ($file=="index.php" || $file==".htaccess" || is_dir($file) || filesize($file)==0 || !strpos($file,".")) continue;
- $kb = filesize($file)/1024;
- if ($kb>=1000) $mb = sprintf("%.2f",$kb/1024);
- else $kb = sprintf("%.2f",$kb);
- $size = (isset($mb))? "$mb MB" : "$kb KB";
- $link[] = "<a href=\"".rawurlencode($file)."\" title=\"Added on ".date("j M Y",filemtime($file))."\">$file</a> $note<span>[$size]</span>";
- }
- closedir($dh);
- clearstatcache();
- natcasesort($link);
- $i = 0;
- echo "<ul id=\"content\">\n";
- foreach ($link as $val) {
- $class = (!($i%2))? "" : " class=\"r2\"";
- echo "\t<li$class>$val</li>\n";
- ++$i;
- }
- echo "</ul>\n";
- source file: dir-listing-files.txt
CSS:
- #content {
- list-style: none;
- padding: 0;
- margin: 1em;
- }
- #content li {
- padding: 0.2em;
- position: relative;
- line-height: 1.5em;
- }
- #content li.r2 {
- background-color: #f2fafe;
- }
- #content a, #content a:link, #content a:visited {
- font-weight: bold;
- text-decoration: none;
- color: #ff987c;
- }
- #content a:hover, #content a:active {
- color: #b7afa3;
- text-decoration: underline;
- }
- #content span {
- position: absolute;
- right: 2px;
- top: 0.5em;
- font-size: 0.8em;
- color: #a2b7f8;
- } * html #content span {right: 30px;}
- 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).