PHP closedir() 函式



PHP 的 closedir() 函式代表“關閉目錄”。此函式用於關閉目錄控制代碼。

要對檔案和資料夾執行任何操作,我們必須開啟包含這些檔案和其他資料夾的目錄。當我們開啟目錄時,opendir() 函式會為我們提供一個目錄控制代碼。它可以用於執行進一步的相關任務。最後,使用 closedir() 函式關閉目錄控制代碼資源。

重要提示

  • 開啟的目錄可以用於多種用途。因此,開啟的資源不會過期,除非您手動關閉它。因此,closedir() 函式透過關閉目錄控制代碼來提高資源效率。
  • 在關閉檔案控制代碼之前,使用 is_resource() 函式確定它是否是資源。這可以避免您遇到錯誤。
  • 如果您嘗試關閉已關閉的目錄控制代碼,則會發生 Uncaught TypeError 錯誤。因此,在關閉它之前,您需要確認已定義的目錄控制代碼是一個資源。

語法

以下是 PHP 的 closedir() 函式的語法:

void closedir ( resource $dir_handle );

引數

以下是 closedir() 函式的必需和可選引數:

序號 引數及描述
1

dir_handle(可選)

這是使用 opendir() 方法先前開啟的目錄的控制代碼。

返回值

該函式不返回值。

PHP 版本

closedir() 函式在 PHP 4 核心版本中引入,並與 PHP 5、PHP 7 和 PHP 8 相容。

示例

在這個示例中,我們將向您展示 PHP closedir() 函式的基本用法。我們將結合使用 opendir() 和 readdir() 函式以及 closedir()。如上所述,要關閉特定目錄,我們必須使用 opendir() 和 readdir() 函式開啟並讀取它。因此,讀取內容後,我們可以使用 closedir() 函式關閉它。

<?php
   // open the directory using opendir()
   $dir = opendir("/var/www/images");

   // Check if the directory handle is valid
   if ($dir) {
      // Loop through the directory and read each file
      while (($file = readdir($dir)) !== false) {
         echo "filename: " . $file . "<br>";
      }
   
      // Close the directory
      closedir($dir);
   
      // Show message that directory is closed
      echo "The directory has been closed.";
   } else {
      echo "Could not open the directory.";
   }
?> 

輸出

這將產生以下結果:

filename: .
filename: ..
filename: logo.gif
filename: mohd.gif
The directory has been closed.

示例

在這個 PHP 示例中,我們將使用 closedir() 函式,並且不會在函式中提及目錄控制代碼。並顯示一條訊息來告知使用者目錄已關閉。

<?php
   $dir = opendir("/var/www/images");

   if ($dir) {
      while (($file = readdir($dir)) !== false) {
         echo "filename: " . $file . "<br>";
      }
   
      // close the directory without mentioning the directory handle
      closedir($dir);
   
      echo "The directory has been closed.";
   } else {
      echo "Could not open the directory.";
   }
?> 

輸出

這將給出以下結果:

filename: .
filename: ..
filename: logo.gif
filename: mohd.gif
The directory has been closed.

示例

在此 PHP 程式碼中,我們將透過確認使用者是否要關閉它來安全地關閉目錄。因此,使用這種方法,我們為使用者提供了額外的保障。顯示一條訊息,表明目錄已關閉。如果 $confirmClose 為 false,則顯示一條訊息,指出目錄未關閉。

<?php
   // open the directory
   $dir = opendir("/var/www/images");

   // check if the directory handle is valid
   if ($dir) {
      // loop over the directory and read each file
      while (($file = readdir($dir)) !== false) {
         echo "Filename: " . $file . "<br>";
      }
    
      // Confirm before closing the directory handle
      $confirmClose = true; 
    
      if ($confirmClose) {
         // close the directory using closedir()
         closedir($dir);
        
         // show message that directory is closed
         echo "The directory has been closed.";

      } else {
         echo "The directory was not closed.";
      }
   } else {
      echo "Could not open the directory.";
   }
?> 

輸出

這將導致以下結果:

Filename: .
Filename: ..
Filename: index.php
Filename: new dir
Filename: images
Filename: my.php
The directory has been closed.

總結

使用完目錄控制代碼後,需要將其關閉。我們可以透過內建的目錄函式 closedir() 來完成此任務。

php_function_reference.htm
廣告