Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

Traversable File Listing

Below the source code for the traversable php file listing you'll find a form to give an example of the output produced.

If directories are listed they are clickable to display that folder's files.

The working version below has been restricted for security reasons.

The script as is shouldn't be used on a public server, but is fine for internal use such as on a local network or computer.


print ("<form action=\"$PHP_SLEF\" method=\"POST\">\n");
print ("Path (optional) <input type=\"text\" name=\"dirname\" value=\"$dirname\"><br>\n");
print ("<input type=\"Submit\" value=\"submit\" name=\"submit\">\n");
print ("</form>\n");

function read_dir($basedir,$thisdir) {
//   $thisdir = eregi_replace("/","\\",$thisdir);
   if ($handle = opendir($thisdir)) {
      print ("<p><b><a href=\"file_listing_alter.php?dirname=$thisdir\">$thisdir</a></b></p>");
      while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
         if (is_dir("$thisdir/$file")) {
               $dir_array[] = $file;
            } else {
               $file_array[] = $file;
            }
         }
    }
   closedir($handle);
   }
   if ($file_array) {
      sort($file_array);
      foreach ($file_array as $key=>$var) {
         if ($basedir == $thisdir) {
            $thisdir_bit = "/";
         } else {
            $basedir_strlen = strlen($basedir);
            $thisdir_bit = substr($thisdir,$basedir_strlen);
         }
         $filesize = filesize("$thisdir/$file_array[$key]");
         $filectime = filectime("$thisdir/$file_array[$key]");
         $filectime = date("Y-m-d H:i:s", $filectime);
         print ("$file_array[$key]<br>");
      }
   }
   if ($dir_array) {
      sort($dir_array);
      foreach ($dir_array as $key=>$var) {
      $nextdir = "$thisdir/$var";
      read_dir($basedir,$nextdir);
      }
   }
}

if ($submit && !$dirname) {
   $dirname = $PATH_TRANSLATED;
   print ("<p>$dirname</p>");
}

if ($dirname) {
   if (is_file($dirname)){
      $dirname = dirname("$dirname");
   }
   $dirname = stripslashes("$dirname");
   read_dir($dirname,$dirname);
}

See the code in action here:

In the version above the path would work, we've disabled it here for security reasons.

Path (optional)

Share this!