Latest web development tutorials

PHP readdir () 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

readdir () function returns the directory under a file name.


grammar

readdir( dir_handle );

参数 描述
dir_handle 可选。指定之前由 opendir() 打开的目录句柄资源。如果该参数未指定,则使用最后一个由 opendir() 打开的链接。

technical details

return value: Returns the filename on success, on failure returns FALSE.
PHP version: 4.0+


PHP Directory PHP Directory Reference