Latest web development tutorials

PHP opendir () function

PHP Directory PHP Directory Reference

Examples

Open a directory, read its contents, and 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);
}
}
?>

result:

filename: cat.gif
filename: dog.gif
filename: horse.gif


Definition and Usage

opendir () function to open a directory handle.


grammar

opendir( path,context );

参数 描述
path 必需。规定要打开的目录路径。
context 可选。规定目录句柄的环境。context 是可修改目录流的行为的一套选项。

technical details

return value: Returns a directory handle resource on success. It failed to return FALSE. If the path is not a valid directory, or due to licensing restrictions or filesystem errors directory can not be opened, then throws E_WARNING level error. You can add in front of the function name '@' to hide opendir () error output.
PHP version: 4.0+
PHP Update Log: PHP 5.0: path parameter supports the ftp: // URL wrappers.


PHP Directory PHP Directory Reference