php 目錄遍歷、刪除 函數(shù)的使用介紹
包括 遍歷該文件夾下的文件,目錄子目錄 讀取當(dāng)前文件下目錄和文件 刪除當(dāng)前文件夾下的目錄子目錄以及文件 以上三個函數(shù)目前還不支持中文文件 中文目錄
<?php
header("Content-type:text/html;charset=utf-8");
/**
* 讀取當(dāng)前目錄下的文件和目錄
*
* @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']);
}
/**
* 遍歷當(dāng)前目錄下的文件和目錄以及子文件夾中目錄
*
* @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);
}
?>
相關(guān)文章
ThinkPHP學(xué)習(xí)筆記(一)ThinkPHP部署
我是在新浪的SAE上安裝ThinkPHP,SAE引擎的,直接安裝源代碼包,然后啟動Memcache和MySql服務(wù)就可以運行成功了。2014-06-06
Zend?Framework框架實現(xiàn)發(fā)送郵件的方法
這篇文章主要介紹了Zend?Framework框架實現(xiàn)發(fā)送郵件的方法,實例分析了Zend?Framework使用smtp郵件類實現(xiàn)郵件發(fā)送的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12

