PHP: List Files in Directory

This snippet will print out all files in a specified directory. I used this code on my old Holocaust History Archive site in 2005 or some such:

<?php
// Define the full path to your folder from root
$path = "/some/k0nsl/dir";

// Open the folder
$dir_handle = @opendir($path) or die("Unable to open $path");

// Loop through the files
while ($file = readdir($dir_handle)) {

if($file == "." || $file == ".." || $file == "index.php" || $file == "stats" || $file == "inc" || $file == "data" || $file == "cgi-bin" || $file == "links" || $file == "skins" || $file == "referer-log" || $file == "index.html" || $file == "sitemap.html" || $file == "default.css" || $file == "bt2_ie5.css" || $file == "style.css" || $file == "bt2_ie6.css" || $file == "bt2_main.css" || $file == "default.css" || $file == ".htaccess" || $file == "default.css" )

continue;
echo "&lt;IMG SRC='./graphs/sys-img/parent.gif'&gt; &lt;a href=\"$file\"&gt;$file&lt;/a&gt;&lt;br /&gt;";

}
// Close
closedir($dir_handle);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *