小編今天沒事寫了目錄想關的函數
包括 遍曆該檔案夾下的檔案,目錄子目錄 讀取當前檔案下目錄和檔案 刪除當前檔案夾下的目錄子目錄以及檔案 以上三個函數目前還不支援中文檔案 中文目錄
複製代碼 代碼如下:
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小編今天沒事寫了目錄想關的函數 包括 遍曆該檔案夾下的檔案,目錄子目錄 讀取當前檔案下目錄和檔案 刪除當前檔案夾下的目錄子目錄以及...