opendir

(PHP 3, PHP 4, PHP 5)

opendir -- 開啟目錄識別碼

說明

resource opendir ( string path [, resource context] )

開啟一個目錄識別碼,可用於之後的 closedir()readdir()rewinddir() 呼叫中。

參數

path

要開啟的目錄路徑

context

context 參數的說明見手冊中的 Streams API 一章。

返回值

若果成功則返回目錄識別碼的 resource,失敗則返回 FALSE

若果 path 不是一個合法的目錄或是因為權限限制或檔案系統錯誤而不能開啟目錄,opendir() 返回 FALSE 並產生一個 E_WARNING 層級的 PHP 錯誤訊息。可以在 opendir() 前面加上「@」符號來抑制錯誤訊息的輸出。

更新日誌

版本說明
5.0.0 path 支援 ftp:// URL wrapper
4.3.0 path 可以是任何支援目錄清單的 URL,不過在 PHP 4 中只有 file:// URL wrapper 支援此功能

範例

例子 1. opendir() 例子

<?php
$dir 
"/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if (
$dh opendir($dir)) {
        while ((
$file readdir($dh)) !== false) {
            echo 
"filename: $file : filetype: " filetype($dir $file) . "\n";
        }
        
closedir($dh);
    }
}
?>

上例的輸出類似於:

filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: cgi : filetype: dir
filename: cli : filetype: dir