国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php 目錄遍歷、刪除 函數的使用介紹

2024-05-04 21:53:28
字體:
來源:轉載
供稿:網友
小編今天沒事寫了目錄想關的函數

包括 遍歷該文件夾下的文件,目錄子目錄 讀取當前文件下目錄和文件 刪除當前文件夾下的目錄子目錄以及文件 以上三個函數目前還不支持中文文件 中文目錄

復制代碼 代碼如下:www.CuoXIn.com

<?/**
* 讀取當前目錄下的文件和目錄
*
* @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("<br />",$arr['dir'])."<br />";
echo "文件有:".implode("<br />",$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);
}
?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜宁县| 磐石市| 鸡泽县| 龙州县| 临高县| 商南县| 万荣县| 南平市| 中卫市| 阿坝| 高碑店市| 巫溪县| 龙南县| 安新县| 南投市| 长子县| 上杭县| 西昌市| 庄河市| 岳阳市| 巴里| 丰顺县| 华亭县| 包头市| 乌苏市| 融水| 阿克陶县| 皋兰县| 定南县| 青河县| 定陶县| 河北区| 固始县| 稷山县| 宜阳县| 邻水| 双江| 邻水| 上栗县| 浑源县| 旬邑县|