Home > Article > Backend Development > How to use opendir in PHP
PHP opendir() function
Example
<?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); } } ?>
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 for directory handles. context is a set of options that modify the behavior of the directory stream. |
Technical details
If successful, the directory handle resource is returned. Returns FALSE on failure. If the path is not a legal directory, or the directory cannot be opened due to licensing restrictions or file system errors, an error of level E_WARNING is thrown. You can hide the error output of opendir() by adding '@' before the function name. | |
---|---|
4.0 | |
PHP 5.0: path parameter support | ftp:// URL encapsulation protocol. |
The above is the detailed content of How to use opendir in PHP. For more information, please follow other related articles on the PHP Chinese website!