兼容PHP5的PHP目錄管理函數(shù)庫
更新時間:2008年07月10日 23:43:03 作者:
php下進(jìn)行目錄的一些操作,經(jīng)常用到的方法
主要能兼容: PHP 5
一、chdir -- 改變目錄
語法:bool chdir ( string directory )
返回值:整數(shù)
函數(shù)種類: 文件存取
內(nèi)容說明:
將 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)語句中會出現(xiàn)“ Warning: chdir(): No such file or directory (errno 2) in ***** on line *”錯誤。
程序代碼
<?php
// current directory
echo getcwd() . "\n";
for($i=1; $i<=2; $i++){
chdir('whoist');
// current directory
echo getcwd() . "\n";
}
?>
二、dir -- directory 類
語法:new dir(string directory);
返回值:類
函數(shù)種類: 文件存取
內(nèi)容說明:
這是一個類似面向?qū)ο蟮念悇e類,用來讀取目錄。當(dāng)目錄參數(shù) directory 打開之后,有二個屬性可用:handle 屬性就像其它非類的函數(shù)所用的 readdir()、rewinddir() 及 closedir();path 屬性則配置打開目錄后的路徑參數(shù)。本類有三個方法 (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)的順序依賴于系統(tǒng)。
注: 本函數(shù)定義了內(nèi)部類 Directory,意味著不能再用同樣的名字定義用戶自己的類。
三、closedir -- 關(guān)閉目錄句柄
語法:void closedir ( resource dir_handle )
返回值:無
函數(shù)種類: 文件存取
內(nèi)容說明:
關(guān)閉由 dir_handle 指定的目錄流。流必須之前被 opendir() 所打開。
例子講解:
程序代碼
<?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 -- 打開目錄句柄
語法:resource opendir ( string path [, resource context] )
返回值:整數(shù)
函數(shù)種類: 文件存取
內(nèi)容說明:
本函數(shù)用來打開目錄資料流。返回的整數(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
一、chdir -- 改變目錄
語法:bool chdir ( string directory )
返回值:整數(shù)
函數(shù)種類: 文件存取
內(nèi)容說明:
將 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)語句中會出現(xiàn)“ Warning: chdir(): No such file or directory (errno 2) in ***** on line *”錯誤。
程序代碼
<?php
// current directory
echo getcwd() . "\n";
for($i=1; $i<=2; $i++){
chdir('whoist');
// current directory
echo getcwd() . "\n";
}
?>
二、dir -- directory 類
語法:new dir(string directory);
返回值:類
函數(shù)種類: 文件存取
內(nèi)容說明:
這是一個類似面向?qū)ο蟮念悇e類,用來讀取目錄。當(dāng)目錄參數(shù) directory 打開之后,有二個屬性可用:handle 屬性就像其它非類的函數(shù)所用的 readdir()、rewinddir() 及 closedir();path 屬性則配置打開目錄后的路徑參數(shù)。本類有三個方法 (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)的順序依賴于系統(tǒng)。
注: 本函數(shù)定義了內(nèi)部類 Directory,意味著不能再用同樣的名字定義用戶自己的類。
三、closedir -- 關(guān)閉目錄句柄
語法:void closedir ( resource dir_handle )
返回值:無
函數(shù)種類: 文件存取
內(nèi)容說明:
關(guān)閉由 dir_handle 指定的目錄流。流必須之前被 opendir() 所打開。
例子講解:
程序代碼
<?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 -- 打開目錄句柄
語法:resource opendir ( string path [, resource context] )
返回值:整數(shù)
函數(shù)種類: 文件存取
內(nèi)容說明:
本函數(shù)用來打開目錄資料流。返回的整數(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() - 移動數(shù)組內(nèi)部指針到最后一個元素,并返回該元素的值
end() 函數(shù)將數(shù)組內(nèi)部指針指向最后一個元素,并返回該元素的值(如果成功)。2011-10-10php操作redis數(shù)據(jù)庫常見方法實(shí)例總結(jié)
這篇文章主要介紹了php操作redis數(shù)據(jù)庫常見方法,結(jié)合實(shí)例形式總結(jié)分析了PHP操作redis數(shù)據(jù)庫的基本安裝、連接、字符串、哈希表、列表、集合等相關(guān)操作技巧,需要的朋友可以參考下2020-02-02有關(guān)phpmailer的詳細(xì)介紹及使用方法
有關(guān)phpmailer的用法,有需要的朋友不妨參考下2013-01-01