PHP 目錄 readdir() 函式



PHP 目錄readdir()函式用於查詢目錄中檔案或資料夾的名稱。顧名思義,readdir 代表“讀取目錄”。此函式基本上一次讀取一個檔案或資料夾的名稱。檔案系統儲存返回條目的順序。該函式被稱為“readdir”,因為它讀取目錄中的內容。

它返回目錄中下一個檔案的名稱。檔名按檔案系統儲存的順序返回。

由於此函式一次僅從目錄中返回一個元素,因此您可能想知道如果存在多個檔案或資料夾,如何一次檢索目錄中的所有專案。為此,您可以執行迴圈或使用 scandir() 方法。

語法

以下是 PHP 目錄readdir()函式的語法 -

string readdir ( resource $dir_handle );

引數

使用readdir()函式所需的引數如下所述 -

序號 引數和描述
1

dir_handle(必填)

之前使用opendir()開啟的目錄控制代碼資源。

返回值

成功時返回檔名,失敗時返回 FALSE。

PHP 版本

首次出現在 core PHP 4 中,readdir()函式在 PHP 5、PHP 7 和 PHP 8 中繼續輕鬆執行。

示例

在此示例中,我們將向您展示如何在使用 opendir() 函式開啟目錄後使用 PHP 目錄readdir()函式使用 while 迴圈。

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

   while (($file = readdir($dir)) !== false) {

      echo "filename: " . $file . "<br />";
   }

   closedir($dir);
?> 

輸出

這將產生以下結果 -

filename: .
filename: ..
filename: logo.gif
filename: mohd.gif

示例

之前我們已經看到如何使用 while 迴圈和readdir()函式檢索檔案,現在我們將看到如何使用 if-else 語句顯示檔名。

<?php
   // declare the directory path (You can replace it with your specific directory path)
   $directory = "/Applications/XAMPP/xamppfiles/htdocs/mac/new dir";

   if(is_dir($directory)){
      if($dir_handle = opendir($directory)){
         while(false !== ($entry = readdir($dir_handle))){
               echo "My Directory Content is: " . $entry . "<br>";
         }
      }
   }else{
      echo "This is not a directory.";
   }
?> 

輸出

這將導致以下結果 -

My Directory Content is: .
My Directory Content is: ..
My Directory Content is: .DS_Store
My Directory Content is: Pictures
My Directory Content is: my_folder
My Directory Content is: index.txt
My Directory Content is: my.txt

示例

在最後一個示例中,您可以在輸出中看到存在 . 或 ..,因此在此示例中,我們將刪除它們以更好地檢視檔案或資料夾。為了實現此功能,我們將只使用 if 條件。

<?php
   // declare the directory path (You can replace it with your specific directory path)
   $directoryPath = "/Applications/XAMPP/xamppfiles/htdocs/mac/new dir";

   if(is_dir($directoryPath)){
      if($handle = opendir($directoryPath)){
         while(false !== ($item = readdir($handle))){
               if ($item != "." && $item != "..") {
                  echo "Item in Directory: " . $item . "<br>";
               }
         }
         closedir($handle); // Close the directory handle
      }
   }else{
      echo "The specified path is not a directory.";
   }

?> 

輸出

此 PHP 程式碼的結果為 -

Item in Directory: .DS_Store
Item in Directory: Pictures
Item in Directory: my_folder
Item in Directory: index.txt
Item in Directory: my.txt

示例

正如我們已經看到如何使用readdir()函式讀取目錄中存在的實體的不同示例。現在我們將只讀取檔案並保留其他內容,如影像和存在的資料夾。

<?php
   // declare the directory path (You can replace it with your specific directory path)
   $directory = "/Applications/XAMPP/xamppfiles/htdocs/mac/new dir";

   // check if the specified path is a directory
   if(is_dir($directory)){
      // open the directory
      if($dir_handle = opendir($directory)){
         // Loop over each entry in the directory
         while(false !== ($entry = readdir($dir_handle))){
               // check if the entry is a file
               if (is_file($directory . "/" . $entry)) {
                  // echo the name of the file
                  echo "File in Directory: " . $entry . "<br>";
               }
         }
         // close the directory handle
         closedir($dir_handle);
      }
   } else {
      // echo an error message if the specified path is not a directory
      echo "The specified path is not a directory.";
   }
?> 

輸出

這將產生以下輸出 -

File in Directory: .DS_Store
File in Directory: index.txt
File in Directory: my.txt

注意

當您呼叫readdir()方法時,會出現兩個不同的專案:一個點 (.) 表示當前目錄,兩個點 (..) 表示父目錄。父目錄是現在直接位於此目錄上方的資料夾。通常,在處理目錄中的檔案時,您不需要使用這些特定專案。

總結

PHP 的readdir()方法用於一次讀取目錄內檔案和目錄的名稱。返回目錄中下一個檔案或資料夾的名稱。此方法可用於按順序列出目錄內容;它包括當前目錄 (.) 和父目錄 (..) 的特殊條目。

php_function_reference.htm
廣告