PHP | dir()(获取目录的实例)

PHP中的dir()函数用于查找目录类的实例。此函数用于读取目录,其中包括以下内容:

null
  • 给定的目录已打开。
  • 这两个属性 手柄 路径 可以使用dir()的函数。
  • handle属性可以与其他目录函数一起使用,例如readdir()、rewinddir()、closedir()。path属性设置为打开的目录的路径
  • 句柄和路径属性都有三种方法:read()、revend()和close()。

语法:

 dir(string $directory, resource $context)

使用的参数: dir()函数接受两个参数。具体说明如下:

  1. $directory: 这是一个必需的参数。它指定要打开的目录。
  2. $context: 这是一个可选参数。它包含对中所有模块的引用 与正则表达式匹配的请求可能需要的目录。

返回值: 成功后,上述函数将返回目录类的一个实例。否则,失败时将返回FALSE。

注:

  1. read方法返回目录项的顺序取决于系统。
  2. 此函数定义内部类目录,这意味着我们将无法使用该名称定义自己的类。

例子: 下面是上述功能的实现:

<?php
// getcwd() function will return
// the current working directory
$directory = dir( getcwd ());
// Exploring directories and their contents
echo "Handle: " . $directory ->handle . "" ;
echo "Path: " . $directory ->path . "" ;
// If the evaluation is true then, the loop will
// continue otherwise any directory entry with name
// equals to FALSE will stop the loop .
while (( $file = $directory ->read()) !== false) {
// printing Filesystem objects/functions with PHP
echo "filename: " . $file . "" ;
}
$directory ->close();
?>


输出:

Handle: Resource id #3
Path: /storage/ssd2/630/2687630/public_html
filename: .
filename: ..
filename: bookkart
filename: index.php
filename: upload.html
filename: hello.html
filename: file-upload-manager.php
filename: tmp.php
filename: raj.php
filename: gfgchecking
filename: gfg.txt

参考: http://php.net/manual/en/function.dir.php

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享