PHP opendir() function
Example
Open a directory, read its content, and then close it:
<?php $dir = "/images/"; // Open the directory and read its content 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: |
If successful, it returns a directory handle resource. If failed, it returns FALSE. 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 Parameters now support ftp:// URL encapsulation protocol. |