Log in

View Full Version : List Contents of Directory With exception



a6april
12-06-2008, 10:25 PM
I am trying to display in a list format the contents of a specific directory. However I would like to exclude specific files for example:
.htaccess
error_log
another_file.html
some_sub_Directory.

Just having a hard time with the the and if else statements, I am assuming that is the way to go, if not please advise, here is the code I am working with:

#######################
<?php

$path = "/Directory Path/To/Folder";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
if($file != "." && $file != "..") {
echo "$i. <a target=_blank href='$file'>$file</a><br />";
$i++;
}
}
closedir($dh);
?>
#########################

BLiZZaRD
12-07-2008, 03:57 PM
<?

//define the path as relative
$path = "/home/yoursite/public_html/whatever";

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

echo "Directory Listing of $path<br/>";

//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!=".." && $file!=".htaccess" && $file!="error_log" && $file!="another_file.html")
echo "<a href='$file'>$file</a><br/>";
}


//closing the directory
closedir($dir_handle);

?>