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
廣告