PHP directory listing for HTML…

โ€”

by

in

Ever need to show the files in a directory to the web, but don’t want to turn indexing on? The following PHP (saved here on my blog but stolen from this usenet post) does that.

Use at your own risk.

<?php
  
  //create the html to display the selection page
  echo ‘<html>’;
  echo ‘<body>’;
  echo ‘<h1>Click on the backup file to save it to your PC.</h1>’;
  echo ‘<ul>’;
  
  
  //open the data directory and read the names of all the files
  //into a list to be displayed on the page
  $handle=opendir(‘/www/htdocs/backups/data/’);
  while (false !== ($file = readdir($handle))){
        //if the list returns . or .. skip the display step
        //and continue looping
        if(($file == “.”) OR ($file == “..”)){continue;}
        //print out the name of the file so that it can be
        //selected for download
        echo ‘<li><a href=”/tmaster/backups/data/’.$file.’”>’.$file.'</a></li>’;
  }
  
  //close the direcotry
  closedir($handle);
  
  echo ‘</ul>’;
  echo ‘</body>’;
  echo ‘</html>’;
  ?>