
- 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 - Flash 訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 程式碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - 除錯
- PHP - 針對 C 開發者
- PHP - 針對 Perl 開發者
- PHP - 框架
- PHP - Core PHP vs. 框架
- 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 目錄 rewinddir() 函式
PHP 目錄 **rewinddir()** 函式將最近開啟的目錄控制代碼的位置重置到開頭,允許你再次讀取目錄內容。這個函式很有用,因為在使用 readdir() 讀取目錄內容後,如果不使用 **rewinddir()** 重置,就無法再次讀取該目錄。而且,可以多次使用 **rewinddir()**。
簡單來說,它會將 dir_handle 給定的目錄流重置到目錄的開頭。
語法
以下是 PHP 目錄 **rewinddir()** 函式的語法:
void rewinddir ( resource $dir_handle );
引數
以下是 **rewinddir()** 函式所需的 引數:
序號 | 引數及說明 |
---|---|
1 |
dir_handle (必需) 先前使用 opendir() 開啟的目錄控制代碼資源。 |
返回值
返回 NULL,表示此函式不返回值。
PHP 版本
在 PHP 4的核心版本中引入,**rewinddir()** 函式與 PHP 5、PHP 7 和 PHP 8 相容良好。
示例
在這個 PHP 程式碼中,我們將開啟目錄並讀取第一個條目,然後使用 PHP 目錄 **rewinddir()** 函式回退並再次讀取第一個條目,最後關閉目錄。
<?php // try to open the directory $dir = opendir("/Applications/XAMPP/xamppfiles/htdocs/mac"); // read the first entry readdir($dir); // rewind to the beginning rewinddir($dir); // read the first entry again echo "Read the first entry: ".readdir($dir); // close the directory closedir($dir); ?>
輸出
這將產生以下結果:
Read the first entry: .
示例
現在我們將使用 **rewinddir()** 函式顯示當前工作目錄或我們給定路徑中存在的所有內容,並使用 while 迴圈遍歷其中的內容。
<?php // Try to open the directory $dir = opendir("/Applications/XAMPP/xamppfiles/htdocs/mac"); echo "Items in my directory are- " . "<br>"; while(false !== ($entry = readdir($dir))){ echo "Item: " . $entry . "<br>"; } rewinddir(); echo "After rewinddir() function, items in my directory are- " . "<br>"; while(false !== ($entry = readdir($dir))){ echo "Item: " . $entry . "<br>"; } ?>
輸出
這段程式碼將產生以下結果:
Items in my directory are- Item: . Item: .. Item: logo.gif Item: .DS_Store Item: index.php Item: new dir Item: images Item: image.gif Item: myfile.txt Item: my.php After rewinddir() function, items in my directory are- Item: . Item: .. Item: logo.gif Item: .DS_Store Item: index.php Item: new dir Item: images Item: image.gif Item: myfile.txt Item: my.php
示例
在下面的 PHP 程式碼中,我們將開啟目錄,使用 readdir() 列出其內容,使用 mkdir() 函式建立一個“Test”目錄,使用 **rewinddir()** 函式回退,然後再次列出內容。
<?php // Try to open the directory $directory = opendir("/Applications/XAMPP/xamppfiles/htdocs"); echo "Contents of 'htdocs' directory are- "."<br>"; while(false !== ($entry = readdir($directory))){ echo $entry . "<br>"; } mkdir("/Applications/XAMPP/xamppfiles/htdocs/Test"); rewinddir($directory); echo "After rewinddir() function, Contents of 'htdocs' directory are- "."<br>"; while(false !== ($entry = readdir($directory))){ echo $entry . "<br>"; } ?>
輸出
這段程式碼將產生以下結果:
Contents of 'htdocs' directory are- . .. favicon.ico .DS_Store index.php applications.html webalizer img dashboard mac bitnami.css After rewinddir() function, Contents of 'htdocs' directory are- . .. favicon.ico .DS_Store index.php Test applications.html webalizer img dashboard mac bitnami.css
示例
現在我們將使用 opendir() 開啟並使用 readdir() 列出兩個目錄(“htdocs”和“mac”)的內容,使用 **rewinddir()** 回退“mac”目錄,然後再次列出兩個目錄的內容。
<?php // open the first directory $directory1 = opendir("/Applications/XAMPP/xamppfiles/htdocs"); // open the second directory $directory2 = opendir("/Applications/XAMPP/xamppfiles/htdocs/mac"); echo "Contents of 'htdocs' directory are- "; while(false !== ($entry = readdir($directory1))){ echo $entry . "<br>"; } echo "Contents of 'mac' directory are- "; while(false !== ($entry = readdir($directory2))){ echo $entry . "<br>"; } // rewind the second directory using rewinddir() rewinddir($directory2); echo "After rewinddir() function, Contents of 'mac' directory are- "; while(false !== ($entry = readdir($directory2))){ echo $entry . "<br>"; } echo "After rewinddir() function, Contents of 'htdocs' directory are- "; while(false !== ($entry = readdir($directory1))){ echo $entry . "<br>"; } ?>
輸出
這段 PHP 程式碼的結果是:
Contents of 'htdocs' directory are- . .. favicon.ico .DS_Store index.php applications.html webalizer img dashboard mac Contents of 'mac' directory are- . .. logo.gif .DS_Store index.php images image.gif myfile.txt After rewinddir() function, Contents of 'mac' directory are- . .. logo.gif .DS_Store index.php images image.gif myfile.txt After rewinddir() function, Contents of 'htdocs' directory are-
優勢
與使用 opendir() 函式讀取目錄內容相比,使用 **rewinddir()** 方法的優勢在於,opendir() 使用新的資源,而 **rewinddir()** 使用以前的資源。因此,**rewinddir()** 優化了資源利用率。
注意
如果沒有提供目錄控制代碼,**rewinddir()** 將重新開啟先前開啟的目錄。如果使用錯誤的控制代碼,將顯示錯誤訊息“未定義變數”。
總結
**rewinddir()** 函式是 PHP 內建的目錄函式。它通常有助於反覆讀取目錄的內容。這是一個非常簡單的函式。