函数名:opendir()
适用版本:PHP 4, PHP 5, PHP 7
函数描述:opendir() 函数用于打开一个目录句柄,用于后续的 readdir()、rewinddir() 和 closedir() 调用。
语法:resource opendir ( string $path [, resource $context ] )
参数:
返回值:成功时返回一个目录句柄资源,失败时返回 false。
示例:
$dir = "/path/to/directory";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: " . $file . "<br>";
}
closedir($dh);
}
}
$dir = "/path/to/directory";
$context = stream_context_create();
if ($dh = opendir($dir, $context)) {
// 处理目录内容
closedir($dh);
}
注意事项: