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

首頁 > 開發 > PHP > 正文

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

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

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

復制代碼 代碼如下:


<?php
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("<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);
}
?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青阳县| 荔浦县| 大竹县| 西安市| 阿坝县| 昌乐县| 洪泽县| 宁远县| 大邑县| 安岳县| 兰西县| 沈阳市| 饶河县| 普兰店市| 淮南市| 乌鲁木齐市| 邢台市| 清丰县| 卫辉市| 威信县| 东源县| 清苑县| 龙岩市| 永平县| 毕节市| 马边| 织金县| 罗山县| 峡江县| 通化市| 苏尼特左旗| 长顺县| 都匀市| 齐齐哈尔市| 永泰县| 田林县| 宁德市| 乌兰察布市| 西丰县| 山阴县| 卓资县|