English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Directory Reference Manual
The opendir() function opens a directory handle
resource opendir(string $path[, resource $context]);
Open a directory handle that can be used in subsequent closedir(), readdir(), and rewinddir() calls.
Serial Number | Parameters and Description |
---|---|
1 | path (required) The directory path to be opened |
2 | context (optional) Specify the environment of the directory handle. context is a set of options that can modify the behavior of the directory stream. |
If successful, it returns a resource handle of the directory, and if it fails, it returns FALSE.
If path is not a valid directory or cannot be opened due to permission restrictions or file system errors, opendir() returns FALSE and generates a PHP E_WARNING level error message. You can add the '@' symbol before opendir() to suppress the output of the error message.
The following is the usage of this function: open a directory, read its contents, and then close it:
<?php $dir = opendir("/var/www/images"); while (($file = readdir($dir)) !== false) { echo "filename: " . $file . "<br" />"; } closedir($dir); ?>
Output result:
filename: . filename: .. filename: logo.gif filename: mohd.gif