PHP dir()函数与示例
PHPdir()方法
dir()函数是目录类的实例,用于读取目录,它包括句柄和路径属性–可用于获取资源ID和目录的路径。两个手柄和路径有read(),rewind()和close()方法。
语法:
dir(directory,context);
参数:
directory–用于指定要打开的目录的路径。
上下文–它是一个可选参数;它定义了上下文(一组可以修改流行为的选项)。
返回值:
成功–将实例返回目录;失败–将返回“FALSE”。
示例:PHP代码演示dir()方法示例
<?php
//当前工作目录的路径
$cwd = dir(getcwd());
echo "Directory handle: " . $cwd->handle . "<br>";
echo "Current path: " . $cwd->path . "<br>";
//reading & printing the filenames using dir()while (($file = $cwd->read()) !== false){
echo "File: " . $file . "<br>";
}
//关闭目录实例
$cwd->close();
?>输出结果
Directory handle: Resource id #5 Current path: /home File: . File: .. File: main.php
参考:PHPdir()函数