PHP dir() 函数
示例
使用 dir() 函数
<?php
$d = dir(getcwd());
echo "句柄: " . $d->handle . "<br>";
echo "路径: " . $d->path . "<br>";
while (($file = $d->read()) !== false){
echo "文件名: " . $file . "<br>";
}
$d->close();
?>
结果
句柄: Resource id #2
路径: /etc/php
文件名: .
文件名: ..
文件名: ajax.gif
文件名: books.xml
文件名: cdcatalog.xml
文件名: cd_catalog.xml
文件名: default.asp
文件名: demo_array.asp
文件名: demo_array.htm
...
...
...
定义和用法
dir() 函数返回 Directory 类的一个实例。该函数用于读取目录,其中包括以下内容
- 给定的目录被打开
- dir() 的两个属性 handle 和 path 可用
- handle 和 path 属性都有三个方法:read()、rewind() 和 close()
语法
dir(directory, context)
参数值
参数 | 描述 |
---|---|
directory | 必需。指定要打开的目录 |
context | 可选。 |
技术详情
返回值 | Directory 类的一个实例。失败时返回 FALSE |
---|---|
PHP 版本 | 4.0+ |
❮ PHP 目录参考