Title: Gettting a Directory Listing
Contributor: Randall Goguen (aka Ranman)
Last Update: Tuesday July 18 21:37 EDT 2000
# 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>
# PHP Directory functions.
int chdir(string directory)
Change the current directory
void closedir([int dir_handle])
Close directory connection identified by the dir_handle
class dir(string directory)
Directory class with properties, handle and class and methods read, rewind and close
string getcwd(void)
Gets the current directory
int opendir(string path)
Open a directory and return a dir_handle
string readdir([int dir_handle])
Read directory entry from dir_handle
void rewinddir([int dir_handle])
Rewind dir_handle back to the start
Anyone who wishes to make additions or changes to this
PHP Tip email them to webmaster@linuxguruz.org
This document is Copyright (c) 1999, 2000 by LinuxGuruz