PHP code to display contents of a directory

To create a seamless include of a folder’s contents (as used here: http://ftp.ewea.org/annual2014/) you can use this code: 

<?php
$dir = "/var/www/html/sites/ftp.ewea.org/annual2014/files/"; // Define the directory you want to get contents from
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) { // Start the loop
                if ($file != "." && $file != ".." && $file != ".htaccess")      // Hide unix and hidden files
                {
                        echo "<a href=\"/annual2014/files/" . $file ;           // Print each document as 
                        echo "\" target=\"_blank\">" . $file . "</a><br />\n";  // a link on its own line
                }
        }
        closedir($dh);
    }
}
?>
 
  1. No comments yet.

  1. No trackbacks yet.