兼容PHP5的PHP目錄管理函數(shù)庫(kù)
一、chdir -- 改變目錄
語(yǔ)法:bool chdir ( string directory )
返回值:整數(shù)
函數(shù)種類(lèi): 文件存取
內(nèi)容說(shuō)明:
將 PHP 的當(dāng)前目錄改為directory。directory:新的當(dāng)前目錄。返回值如果成功則返回 TRUE,失敗則返回 FALSE。
例子講解:
程序代碼
<?php
// current directory
echo getcwd() . "\n";
chdir('public_html');
// current directory
echo getcwd() . "\n";
?>
輸出結(jié)果為:
/home/vincent
/home/vincent/public_html
注意:循環(huán)語(yǔ)句中會(huì)出現(xiàn)“ Warning: chdir(): No such file or directory (errno 2) in ***** on line *”錯(cuò)誤。
程序代碼
<?php
// current directory
echo getcwd() . "\n";
for($i=1; $i<=2; $i++){
chdir('whoist');
// current directory
echo getcwd() . "\n";
}
?>
二、dir -- directory 類(lèi)
語(yǔ)法:new dir(string directory);
返回值:類(lèi)
函數(shù)種類(lèi): 文件存取
內(nèi)容說(shuō)明:
這是一個(gè)類(lèi)似面向?qū)ο蟮念?lèi)別類(lèi),用來(lái)讀取目錄。當(dāng)目錄參數(shù) directory 打開(kāi)之后,有二個(gè)屬性可用:handle 屬性就像其它非類(lèi)的函數(shù)所用的 readdir()、rewinddir() 及 closedir();path 屬性則配置打開(kāi)目錄后的路徑參數(shù)。本類(lèi)有三個(gè)方法 (method):read、rewind 與 close。
class dir {
dir ( string directory )
string path
resource handle
string read ( void )
void rewind ( void )
void close ( void )
}
例子講解:
程序代碼
<?php
$d = dir("/etc/php5");
echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
echo $entry."\n";
}
$d->close();
?>
輸出結(jié)果為:
Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli
注: read 方法返回的目錄項(xiàng)的順序依賴(lài)于系統(tǒng)。
注: 本函數(shù)定義了內(nèi)部類(lèi) Directory,意味著不能再用同樣的名字定義用戶(hù)自己的類(lèi)。
三、closedir -- 關(guān)閉目錄句柄
語(yǔ)法:void closedir ( resource dir_handle )
返回值:無(wú)
函數(shù)種類(lèi): 文件存取
內(nèi)容說(shuō)明:
關(guān)閉由 dir_handle 指定的目錄流。流必須之前被 opendir() 所打開(kāi)。
例子講解:
程序代碼
<?php
$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 -- 打開(kāi)目錄句柄
語(yǔ)法:resource opendir ( string path [, resource context] )
返回值:整數(shù)
函數(shù)種類(lèi): 文件存取
內(nèi)容說(shuō)明:
本函數(shù)用來(lái)打開(kāi)目錄資料流。返回的整數(shù)是可供其它目錄函數(shù)closedir(),readdir() 和 rewinddir() 操作的 handle。如果成功則返回目錄句柄的resource,失敗則返回 FALSE。
例子講解:
程序代碼
<?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);
}
}
?>
輸出結(jié)果為:
filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: cgi : filetype: dir
filename: cli : filetype: dir
相關(guān)文章
PHP+FLASH實(shí)現(xiàn)上傳文件進(jìn)度條相關(guān)文件 下載
PHP+FLASH實(shí)現(xiàn)上傳文件進(jìn)度條相關(guān)文件 下載...2007-07-07PHP引擎php.ini參數(shù)優(yōu)化深入講解
這篇文章主要介紹了PHP引擎php.ini參數(shù)優(yōu)化深入講解,php.ini設(shè)置可以提高php的響應(yīng)速度,有感興趣的同學(xué)可以學(xué)習(xí)下2021-03-03php數(shù)組函數(shù)序列之end() - 移動(dòng)數(shù)組內(nèi)部指針到最后一個(gè)元素,并返回該元素的值
end() 函數(shù)將數(shù)組內(nèi)部指針指向最后一個(gè)元素,并返回該元素的值(如果成功)。2011-10-10常見(jiàn)的PHP五種設(shè)計(jì)模式小結(jié)
設(shè)計(jì)模式 一書(shū)將設(shè)計(jì)模式引入軟件社區(qū),該書(shū)的作者是 Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides Design(俗稱(chēng) “四人幫”)。2011-03-03php操作redis數(shù)據(jù)庫(kù)常見(jiàn)方法實(shí)例總結(jié)
這篇文章主要介紹了php操作redis數(shù)據(jù)庫(kù)常見(jiàn)方法,結(jié)合實(shí)例形式總結(jié)分析了PHP操作redis數(shù)據(jù)庫(kù)的基本安裝、連接、字符串、哈希表、列表、集合等相關(guān)操作技巧,需要的朋友可以參考下2020-02-02有關(guān)phpmailer的詳細(xì)介紹及使用方法
有關(guān)phpmailer的用法,有需要的朋友不妨參考下2013-01-01php實(shí)現(xiàn)的簡(jiǎn)單檢驗(yàn)登陸類(lèi)
這篇文章主要介紹了php實(shí)現(xiàn)的簡(jiǎn)單檢驗(yàn)登陸類(lèi),可實(shí)現(xiàn)基本的php數(shù)據(jù)庫(kù)查詢(xún)及密碼匹配的功能,需要的朋友可以參考下2015-06-06