色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php 目錄遍歷、刪除 函數(shù)的使用介紹

小編今天沒事寫了目錄想關(guān)的函數(shù) 

   包括 遍歷該文件夾下的文件,目錄子目錄  讀取當前文件下目錄和文件  刪除當前文件夾下的目錄子目錄以及文件 以上三個函數(shù)目前還不支持中文文件 中文目錄
復制代碼 代碼如下:
<?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);
}
?>

php技術(shù)php 目錄遍歷、刪除 函數(shù)的使用介紹,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 双流县| 安岳县| 泗阳县| 苍溪县| 内乡县| 岑溪市| 柳州市| 南皮县| 白朗县| 福鼎市| 日照市| 福贡县| 泰兴市| 苗栗市| 永修县| 徐州市| 南城县| 苍梧县| 凭祥市| 休宁县| 兰西县| 大化| 东乌| 称多县| 临沧市| 彭泽县| 宝兴县| 巨野县| 宜兴市| 郁南县| 高阳县| 金阳县| 和龙市| 故城县| 黄梅县| 林西县| 霍山县| 宜都市| 衢州市| 闸北区| 唐海县|