PHP 目录 dir() 函数



PHP 目录dir() 函数用于创建目录类的对象。它生成一个目录类句柄,可用于读取和倒回目录的内容(文件和文件夹)。此外,我们还可以关闭目录句柄。

dir() 函数打开一个目录句柄并返回一个对象。该对象包含三种方法,即:
  • read(): 要获取目录中每个文件和文件夹的列表(而不是子文件夹),请使用 read() 函数。
  • rewind(): 要再次开始 read() 过程,请先使用此函数。
  • close(): 要停止使用目录句柄,请使用 close() 函数。

语法

以下是 PHP 目录dir() 函数的语法:

dir(string $directory, ?resource $context = null): Directory|false

参数

以下是dir() 函数的必需和可选参数:

序号 参数及说明
1

$directory(必需)

要打开的目录路径。

2

$context (可选)

上下文流资源。默认值为 null。

返回值

目录函数dir()成功时返回目录句柄,失败时返回 FALSE。

PHP 版本

dir() 函数最初作为核心 PHP 4 的一部分引入,并且与 PHP 5、PHP 7、PHP 8 兼容良好。

示例

以下是 PHP 目录dir() 函数的基本示例:

<php
   // Open the directory
   $directory = dir("/PhpProjects"); 

   // Read entries
   while (($entry = $directory->read()) !== false) { 
      // Print each entry
      echo $entry . "<br>"; 
   }

   // Close the directory
   $directory->close(); 
?>

输出

这将生成以下输出:

.
..
index.php
README.md
images
css
js

示例

在此 PHP 代码示例中,我们将使用dir() 函数列出目录(/var/www 在我们的例子中)的内容,并显示有关该目录中每个条目的信息。它使用 while 循环读取目录中的每个条目。循环在没有更多条目可读时停止。读取所有条目后,目录句柄将使用 close() 方法关闭。

<?php
   $d = dir("/var/www/");
   echo "Handle: " . $d->handle . "\n";
   echo "Path: " . $d->path . "\n";
   
   while (false !== ($entry = $d->read())) {
      echo $entry."\n";
   }
   $d->close();
?> 

这将产生以下结果:

Handle: Resource id #5
Path: /var/www
..
.
html

示例

在此示例中,我们将展示如何在 PHP 中使用dir() 函数列出目录的内容。因此,我们将只打开一个名为“new dir”的目录,该目录应位于当前目录中。然后它列出此目录的所有内容(文件和文件夹),并将每个实体的名称显示为结果。

<?php
   // open the directory
   $directory_handle = dir("./new dir");

   // check if the directory handle is valid or not
   if ($directory_handle) {
      echo "Contents of the directory:<br>";

      // Loop over every entry in the directory
      while (($entry = $directory_handle->read()) !== false) {
         echo $entry . "<br>"; // Output each entry
      }
    
      // close the directory handle using close()
      $directory_handle->close();
   } else {
      echo "Failed to open directory.";
   }
?> 

输出

这将产生以下结果:

Contents of the directory:
.
..
Pictures
my_folder
index.txt
PHP
my.txt

示例

在下面的 PHP 代码中,我们将使用 dir 类的所有三种方法。因此,基本上我们将列出给定目录中可用的内容,并使用rewind() 函数对其进行倒回,然后使用close() 函数将其关闭。

<?php
   $directory_handle = dir("./images/");
   echo "Directory Handle: " . $directory_handle->handle . "<br>";
   echo "Directory Path: " . $directory_handle->path . "<br>";

   // loop to read and output each entry in the directory
   while ($entry = $directory_handle->read()) {
      echo "Entry: " . $entry . "<br>";
   }

   // Rewind the directory handle to start reading from the beginning again
   $directory_handle->rewind();

   // Loop again to read and output each entry in the directory
   while ($entry = $directory_handle->read()) {
      echo "Entry after rewind: " . $entry . "<br>";
   }

   // Close the directory handle
   $directory_handle->close();
?> 

输出

这将生成以下输出:

Directory Handle: Resource id #3
Directory Path: ./images/
Entry: .
Entry: ..
Entry: shankar.jpg
Entry: robot.jpg
Entry: hanuman.jpg
Entry after rewind: .
Entry after rewind: ..
Entry after rewind: shankar.jpg
Entry after rewind: robot.jpg
Entry after rewind: hanuman.jpg

总结

PHP 中的dir() 函数打开一个目录并返回一个句柄,该句柄可用于查看其内容。使用 read() 迭代条目。要关闭,请使用 close() 函数。

因此,我们可以说此函数最适合基本目录操作。

php_function_reference.htm
广告

© . All rights reserved.