
- PHP 教程
- PHP - 首頁
- PHP - 路線圖
- PHP - 簡介
- PHP - 安裝
- PHP - 歷史
- PHP - 特性
- PHP - 語法
- PHP - Hello World
- PHP - 註釋
- PHP - 變數
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 變數
- PHP - 常量
- PHP - 魔術常量
- PHP - 資料型別
- PHP - 型別轉換
- PHP - 型別混合
- PHP - 字串
- PHP - 布林值
- PHP - 整數
- PHP - 檔案與 I/O
- PHP - 數學函式
- PHP - Heredoc 和 Nowdoc
- PHP - 複合型別
- PHP - 檔案包含
- PHP - 日期和時間
- PHP - 標量型別宣告
- PHP - 返回型別宣告
- PHP 運算子
- PHP - 運算子
- PHP - 算術運算子
- PHP - 比較運算子
- PHP - 邏輯運算子
- PHP - 賦值運算子
- PHP - 字串運算子
- PHP - 陣列運算子
- PHP - 條件運算子
- PHP - 展開運算子
- PHP - 空值合併運算子
- PHP - 鍵值比較運算子
- PHP 控制語句
- PHP - 決策
- PHP - If…Else 語句
- PHP - Switch 語句
- PHP - 迴圈型別
- PHP - For 迴圈
- PHP - Foreach 迴圈
- PHP - While 迴圈
- PHP - Do…While 迴圈
- PHP - Break 語句
- PHP - Continue 語句
- PHP 函式
- PHP - 函式
- PHP - 函式引數
- PHP - 按值傳遞
- PHP - 按引用傳遞
- PHP - 預設引數
- PHP - 具名引數
- PHP - 可變引數
- PHP - 返回值
- PHP - 傳遞函式
- PHP - 遞迴函式
- PHP - 型別提示
- PHP - 變數作用域
- PHP - 嚴格型別
- PHP - 匿名函式
- PHP - 箭頭函式
- PHP - 可變函式
- PHP - 區域性變數
- PHP - 全域性變數
- PHP 超全域性變數
- PHP - 超全域性變數
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 檔案處理
- PHP - 檔案處理
- PHP - 開啟檔案
- PHP - 讀取檔案
- PHP - 寫入檔案
- PHP - 檔案是否存在
- PHP - 下載檔案
- PHP - 複製檔案
- PHP - 追加檔案
- PHP - 刪除檔案
- PHP - 處理 CSV 檔案
- PHP - 檔案許可權
- PHP - 建立目錄
- PHP - 列出檔案
- 面向物件的 PHP
- PHP - 面向物件程式設計
- PHP - 類和物件
- PHP - 建構函式和解構函式
- PHP - 訪問修飾符
- PHP - 繼承
- PHP - 類常量
- PHP - 抽象類
- PHP - 介面
- PHP - 特性
- PHP - 靜態方法
- PHP - 靜態屬性
- PHP - 名稱空間
- PHP - 物件迭代
- PHP - 封裝
- PHP - Final 關鍵字
- PHP - 過載
- PHP - 克隆物件
- PHP - 匿名類
- PHP Web 開發
- PHP - Web 概念
- PHP - 表單處理
- PHP - 表單驗證
- PHP - 表單郵件/URL
- PHP - 完整表單
- PHP - 檔案包含
- PHP - GET 和 POST
- PHP - 檔案上傳
- PHP - Cookie
- PHP - Session
- PHP - Session 選項
- PHP - 傳送郵件
- PHP - 淨化輸入
- PHP - Post-Redirect-Get (PRG)
- PHP - 快閃記憶體訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 編碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - Bug 除錯
- PHP - 針對 C 開發人員
- PHP - 針對 PERL 開發人員
- PHP - 框架
- PHP - Core PHP 與框架
- PHP - 設計模式
- PHP - 過濾器
- PHP - JSON
- PHP - 異常
- PHP - 特殊型別
- PHP - 雜湊
- PHP - 加密
- PHP - is_null() 函式
- PHP - 系統呼叫
- PHP - HTTP 認證
- PHP - 交換變數
- PHP - Closure::call()
- PHP - 過濾後的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 期望
- PHP - Use 語句
- PHP - 整數除法
- PHP - 已棄用的特性
- PHP - 已移除的擴充套件和 SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 程序
- PHP - PDO 擴充套件
- PHP - 內建函式
- PHP 有用資源
- PHP - 速查表
- PHP - 問答
- PHP - 快速指南
- PHP - 線上編譯器
- PHP - 有用資源
- PHP - 討論
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()方法用於一次讀取目錄內檔案和目錄的名稱。返回目錄中下一個檔案或資料夾的名稱。此方法可用於按順序列出目錄內容;它包括當前目錄 (.) 和父目錄 (..) 的特殊條目。