Open a directory, read its contents, 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.giffilename: dog.giffilename: horse.gifThe opendir() function opens a directory handle.
opendir( path,context );
parameter | describe |
---|---|
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. |
Return value: | 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 E_WARNING level error is thrown. You can hide the error output of opendir() by prepending '@' to the function name. |
---|---|
PHP version: | 4.0+ |
PHP change log: | PHP 5.0: The path parameter supports the ftp:// URL encapsulation protocol. |