php 目錄遍歷、刪除 函數(shù)的使用介紹_PHP教程
推薦:php無限極分類實(shí)現(xiàn)的兩種解決方法本篇文章介紹了,在php中無限極分類實(shí)現(xiàn)的兩種解決方法。需要的朋友參考下
小編今天沒事寫了目錄想關(guān)的函數(shù) 包括 遍歷該文件夾下的文件,目錄子目錄 讀取當(dāng)前文件下目錄和文件 刪除當(dāng)前文件夾下的目錄子目錄以及文件 以上三個(gè)函數(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);
}
?>
分享:基于php偽靜態(tài)的實(shí)現(xiàn)詳細(xì)介紹本篇文章介紹了,基于php偽靜態(tài)的實(shí)現(xiàn)詳細(xì)分析。需要的朋友參考下
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-php 目錄遍歷、刪除 函數(shù)的使用介紹
。