PHP opendir() Function
Example
Open a directory, read its contents, and then close it:
<?php $dir = "/images/"; // Open the 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
Definition and Usage
The opendir() function opens a directory handle.
Syntax
opendir(path,context);
Parameters | Description |
---|---|
path | Required. Specifies the directory path to be opened. |
context | Optional. Specifies the environment of the directory handle.context It is a set of options that modify the behavior of the directory stream. |
Technical Details
Return Value: |
Returns a directory handle resource on success. Returns FALSE on failure. If the path is not a valid directory, or the directory cannot be opened due to permission restrictions or file system errors, an E_WARNING level error is thrown. You can hide the error output of opendir() by adding '@' before the function name. |
---|---|
PHP Version: | 4.0+ |
PHP Update Log: | PHP 5.0:path The parameter now supports ftp:// URL encapsulation protocol. |