Open a directory, read its contents, and subsequently 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 |
The opendir()
function opens a directory and returns a directory handle.
opendir(path, context) |
Parameter |
Description |
path |
Required. Specifies the path of the directory to be opened. |
context |
Optional. Specifies the context for the directory handle, which is a set of options that can alter the behavior of the stream. |
Return Value: |
Returns the directory handle resource on success, or |
PHP Version: |
4.0+ |
PHP Changelog: |
PHP 5.0: The path parameter now supports the ftp:// URL wrapper. |