here are some directory functions you may find useful
chdir (PHP3 , PHP4 )change directoryint chdir (string directory)
Changes PHP’s current directory to directory. Returns FALSE if unable to change directory, TRUE
otherwise.
dir (PHP3 , PHP4 )directory classnew dir (string directory)
A pseudo-object oriented mechanism for reading a directory. The given directory is opened. Two
properties are available once directory has been opened. The handle property can be used with other
directory functions such as readdir(), rewinddir() and closedir(). The path property is set to path the
directory that was opened. Three methods are available: read, rewind and close.
Example 1. Dir() Example
CODE
$d = dir("/etc");
echo "Handle: ".$d->handle."<br>\n";
echo "Path: ".$d->path."<br>\n";
while($entry=$d->read()) {
echo $entry."<br>\n";
}
$d->close();
closedir (PHP3 , PHP4 )close directory handlevoid closedir (int dir_handle)
Closes the directory stream indicated by dir_handle. The stream must have previously been opened by
opendir().
opendir (PHP3 , PHP4 )open directory handle234
Directories
int opendir (string path)
Returns a directory handle to be used in subsequent closedir(), readdir(), and rewinddir() calls.
readdir (PHP3 , PHP4 )read entry from directory handlestring readdir (int dir_handle)
Returns the filename of the next file from the directory. The filenames are not returned in any particular
order.
Example 1. List all files in the current directory
CODE
<?php
$handle=opendir(’.’);
echo "Directory handle: $handle\n";
echo "Files:\n";
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
?>
Note that readdir() will return the . and .. entries. If you don’t want these, simply strip them out:
Example 2. List all files in the current directory and strip out . and ..
CODE
<?php
$handle=opendir(’.’);
while ($file = readdir($handle)) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
?>
rewinddir (PHP3 , PHP4 )rewind directory handlevoid rewinddir (int dir_handle)
Resets the directory stream indicated by dir_handleto the beginning of the directory.