php 目錄遍曆、刪除 函數的使用介紹_PHP教程

來源:互聯網
上載者:User
小編今天沒事寫了目錄想關的函數

包括 遍曆該檔案夾下的檔案,目錄子目錄 讀取當前檔案下目錄和檔案 刪除當前檔案夾下的目錄子目錄以及檔案 以上三個函數目前還不支援中文檔案 中文目錄
複製代碼 代碼如下:
header("Content-type:text/html;charset=utf-8");
/**
* 讀取目前的目錄下的檔案和目錄
*
* @param string $path 路徑
* @return array 所有滿足條件的檔案
*/
function tlist($path){
$path = iconv('utf-8', 'gbk', $path);
if(!is_dir($path)){
throw new Exception($path."不是目錄");
}
$arr = array('dir'=>array(),'file'=>array());
$hd = opendir($path);
while(($file = readdir($hd))!==false){
if($file=="."||$file=="..") {continue;}
if(is_dir($path."/".$file)){
$arr['dir'][] = iconv('gbk','utf-8',$file);
}else if(is_file($path."/".$file)){
$arr['file'][] = iconv('gbk','utf-8',$file);
}
}
closedir($hd);
echo "目錄有:".implode("
",$arr['dir'])."
";
echo "檔案有:".implode("
",$arr['file']);
}
/**
* 遍曆目前的目錄下的檔案和目錄以及子檔案夾中目錄
*
* @param string $path 路徑
* @return array 所有滿足條件的檔案
*/
function blist($path){
if(!is_dir(iconv("utf-8","gbk",$path))){
throw new Exception("檔案夾".$path."不存在或者不是檔案");
}
$arr = array();
$hd = opendir(iconv("utf-8","gbk",$path));
while(($file = readdir($hd))!==false){
if($file=="."||$file=="..") {continue;}
$newpath=iconv('utf-8', 'gbk', $path) .'/'.$file;
if(is_dir($newpath)){
$arr[] = blist($path."/".$file);
}else if(is_file($newpath)){
$arr[] = iconv('gbk','utf-8',$file);
}
}
closedir($hd);
return $arr;
}
/**
* 刪除目錄下的檔案以及子目錄
* #param string $path 路徑
* #return string 刪除成功返回true 失敗返回false;
*/
function dirDel($path){
if(!is_dir($path)){
throw new Exception($path."輸入的不是有效目錄");
}
$hand = opendir($path);
while(($file = readdir($hand))!==false){
if($file=="."||$file=="..") continue;
if(is_dir($path."/".$file)){
dirDel($path."/".$file);
}else{
@unlink($path."/".$file);
}

}
closedir($hand);
@rmdir($path);
}
?>

http://www.bkjia.com/PHPjc/326921.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326921.htmlTechArticle小編今天沒事寫了目錄想關的函數 包括 遍曆該檔案夾下的檔案,目錄子目錄 讀取當前檔案下目錄和檔案 刪除當前檔案夾下的目錄子目錄以及...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.