CGX Blog We'll see where this takes us.

14Apr/100

PHP Directory Listing

Have you ever had a directory on your website with a bunch of files for people to download? I think the standard directory listing is really ugly...

Old, Ugly List Style

How a file list looks right now

Today I wanted to create a nice template for a directory that has files for download. The critical piece was having a single script that will work on any templated page to maintain the feel of the rest of the website.

All of this information was available on other forums, I just put it all together into a simple script that met my needs - it is copied below for your enjoyment:

<?php
 
$files = array();
 
/* open the current directory by opendir */
if ($handle = opendir('.')) {
 
  /* as long as there is another file, read its path and store to $file */
  while (false !== ($file = readdir($handle))) { 
 
    /* do not list '.', '..', or 'index.php' */
    if ($file != "." && $file != ".." && $file != "index.php") { 
 
      $files[] = $file;
 
    }
 
  }
 
  closedir($handle);
 
}
 
asort($files);
 
foreach ($files as $name) {
 
  echo "<a href=\"$name\" alt=\"$name\">$name</a><br /><br /> \n";
 
}
 
?>

Now, you'll get something like this:

Formatted Directory Listing

A formatted directory list

Its as simple as that.  Just place this script in between the "Changeable Content" section of your templated page and voila, a list of all the files you want with the ability to exclude files you don't want listed.

Enjoy,
Gilman

Filed under: HTML, PHP Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


*

No trackbacks yet.