Show files in a directory

Information for PHP. Hypertext Preprocessor
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

Show files in a directory

Post by dzcadii » Sun Mar 01, 2015 6:26 pm

Easy way to show files in a directory:

Code: Select all

<?
if ($handle = opendir('.')) {
	echo "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"; 
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != "..") { 
            echo "<tr><td>$file</td></tr><tr><td><a href=\"$file\" target=\"_blank\"><img src=\"$file\" border=\"0\"> </a></td></tr>";
        } 
    }
	echo "</table>";
    closedir($handle); 
} 
?>

you can change

Code: Select all

echo "<tr><td>$file</td></tr><tr><td><a href=\"$file\" target=\"_blank\"><img src=\"$file\" border=\"0\"> </a></td></tr>";
to suit your needs


if you do not want a specific file shown (IE: index.php)
then change:

Code: Select all

if ($file != "." && $file != "..") { 
to

Code: Select all

if ($file != "." && $file != ".." && $file != "index.php") { 

Post Reply