相容PHP5的PHP目錄管理函數庫_PHP教程

來源:互聯網
上載者:User
主要能相容: PHP 5
一、chdir -- 改變目錄
文法:bool chdir ( string directory )
傳回值:整數
函數種類: 檔案存取
內容說明:
將 PHP 的目前的目錄改為directory。directory:新的目前的目錄。傳回值如果成功則返回 TRUE,失敗則返回 FALSE。
例子講解:

程式碼
// current directory
echo getcwd() . "\n";
chdir('public_html');
// current directory
echo getcwd() . "\n";
?>


輸出結果為:
/home/vincent
/home/vincent/public_html

注意:迴圈語句中會出現“ Warning: chdir(): No such file or directory (errno 2) in ***** on line *”錯誤。


程式碼
// current directory
echo getcwd() . "\n";
for($i=1; $i<=2; $i++){
chdir('whoist');
// current directory
echo getcwd() . "\n";
}
?>


二、dir -- directory 類
文法:new dir(string directory);
傳回值:類
函數種類: 檔案存取
內容說明:
這是一個類似物件導向的類別類,用來讀取目錄。當目錄參數 directory 開啟之後,有二個屬性可用:handle 屬性就像其它非類的函數所用的 readdir()、rewinddir() 及 closedir();path 屬性則配置開啟目錄後的路徑參數。本類有三個方法 (method):read、rewind 與 close。
class dir {
dir ( string directory )
string path
resource handle
string read ( void )
void rewind ( void )
void close ( void )
}
例子講解:

程式碼
$d = dir("/etc/php5");
echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
echo $entry."\n";
}
$d->close();
?>


輸出結果為:
Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli

注: read 方法返回的目錄項的順序依賴於系統。
注: 本函數定義了內部類 Directory,意味著不能再用同樣的名字定義使用者自己的類。

三、closedir -- 關閉目錄控制代碼
文法:void closedir ( resource dir_handle )
傳回值:無
函數種類: 檔案存取
內容說明:
關閉由 dir_handle 指定的目錄流。流必須之前被 opendir() 所開啟。
例子講解:

程式碼
$dir = "/etc/php5/";
// Open a known directory, read directory into variable and then close
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$directory = readdir($dh);
closedir($dh);
}
}
?>


四、opendir -- 開啟目錄控制代碼
文法:resource opendir ( string path [, resource context] )
傳回值:整數
函數種類: 檔案存取
內容說明:
本函數用來開啟目錄資料流。返回的整數是可供其它目錄函式closedir(),readdir() 和 rewinddir() 操作的 handle。如果成功則返回目錄控制代碼的resource,失敗則返回 FALSE。
例子講解:

程式碼
$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

http://www.bkjia.com/PHPjc/319221.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319221.htmlTechArticle主要能相容:PHP5 一、chdir--改變目錄 文法:boolchdir(stringdirectory) 傳回值:整數 函數種類:檔案存取 內容說明: 將PHP的目前的目錄改為director...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.