Page 1 of 1

Gettting a Directory Listing

New postPosted: Thu Mar 27, 2008 10:34 pm
by Ranman
Code: [Select all] [Expand/Collapse] [Download] (Untitled)
# Here are two examples of reading directorys. The first gets
# a simple directory listing. The second gets all files and
# directory that end with 'gif'.
#
# The @ in front of the opendir() will suppress, if any, a PHP
# error warning from, in this case, opendir() and just print
# the "Unable to open $path" message to the web browser if the
# directory doesn't exsist for example.

<html><body><?
$path = '/usr';
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<BR>";
while ($file = readdir($dir_handle)) {
        echo "$file<br>";
}
closedir($dir_handle);

$path = 'images/';
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "Directory Listing of $path<BR>";
while($file = readdir($dir_handle)) {
  if (substr($file, -3) == 'gif') {
    echo "$file<br>";
} }
closedir($dir_handle);
?></body></html>