PHP opendir()Function

PHP DirectoryPHP Directory Reference Manual

Examples

Open a directory, read its contents, and then close it:

<?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 open.
context Optional. Specifies the context of the directory handle. context is a set of options that can modify the behavior of the directory stream.

Technical Details

Return value: Returns a directory handle resource on success, or FALSE on failure. If the path is not a valid directory, or the directory cannot be opened due to permission restrictions or filesystem errors, an E_WARNING level error is thrown. You can hide opendir()'s error output by adding '@' before the function name.
PHP Version: 4.0+
PHP Changelog: PHP 5.0: The path parameter supportsftp://URL wrapper protocols.


PHP DirectoryPHP Directory Reference Manual Other Extensions