PHP opendir() Function
Definition
The opendir() function opens a directory handle.
Syntax
PHP opendir() Function has the following syntax.
opendir(path,context);
Parameter
Parameter | Is Required | Description |
---|---|---|
path | Required. | Directory path to be opened |
context | Optional. | Context of the directory handle. |
Return
PHP opendir() Function returns the directory handle resource on success. FALSE on failure.
Example
Open a directory, read its contents, then close:
<?php// w w w. j a v a 2s .c o m
$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 . "\n";
}
closedir($dh);
}
}
?>