PHP readdir()Functions
PHP Directory Reference Manual
Examples
Open a directory, read its contents, then close:
<?php
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>
Result:
filename: cat.gif
filename: dog.gif
filename: horse.gif
filename: dog.gif
filename: horse.gif
Definition and Usage
The readdir() function returns the filename of the next file in the directory.
Syntax
readdir(dir_handle);
| Parameters | Description |
|---|---|
| dir_handle | Optional. Specifies the directory handle resource previously opened byopendir()opendir(). If this parameter is not specified, the last link opened by opendir() is used. |
Technical Details
| Return Value: | Returns the filename on success, or FALSE on failure. |
|---|---|
| PHP Version: | 4.0+ |
PHP Directory Reference Manual Other Extensions