PHP遍歷文件夾與文件類(lèi)及處理類(lèi)用法實(shí)例

字號(hào):


    本文實(shí)例講述了PHP遍歷文件夾與文件類(lèi)及處理類(lèi)用法,非常具有實(shí)用價(jià)值。分享給大家供大家參考。具體方法如下:
    FindFile.class.php類(lèi)文件用于遍歷目錄文件,具體代碼如下:
    <?php
    /** 遍歷文件夾及文件類(lèi)
    * Date: 2013-03-21
    * Author: fdipzone
    * Ver: 1.0
    */
    class FindFile{
    public $files = array(); // 存儲(chǔ)遍歷的文件
    protected $maxdepth; // 搜尋深度,0表示沒(méi)有限制
    /* 遍歷文件及文件夾
    * @param String $spath 文件夾路徑
    * @param int $maxdepth 搜尋深度,默認(rèn)搜尋全部
    */
    public function process($spath, $maxdepth=0){
    if(isset($maxdepth) && is_numeric($maxdepth) && $maxdepth>0){
    $this->maxdepth = $maxdepth;
    }else{
    $this->maxdepth = 0;
    }
    $this->files = array();
    $this->traversing($spath); // 遍歷
    }
    /* 遍歷文件及文件夾
    * @param String $spath 文件夾路徑
    * @param int $depth 當(dāng)前文件夾深度
    */
    private function traversing($spath, $depth=1){
    if($handle = opendir($spath)){
    while(($file=readdir($handle))!==false){
    if($file!='.' && $file!='..'){
    $curfile = $spath.'/'.$file;
    if(is_dir($curfile)){ // dir
    if($this->maxdepth==0 || $depth<$this->maxdepth){ // 判斷深度
    $this->traversing($curfile, $depth+1);
    }
    }else{ // file
    $this->handle($curfile);
    }
    }
    }
    closedir($handle);
    }
    }
    /** 處理文件方法
    * @param String $file 文件路徑
    */
    protected function handle($file){
    array_push($this->files, $file);
    }
    }
    ?>
    UnsetBom.class.php用于清除utf8+bom文件的bom,即頭三個(gè)字節(jié) 0xEF 0xBB 0xBF,繼承FindFile類(lèi),具體代碼如下:
    <?php
    /** 遍歷所有文件,清除utf8+bom 0xEF 0xBB 0xBF
    * Date: 2013-03-21
    * Author: fdipzone
    * Ver: 1.0
    */
    class UnsetBom extends FindFile{
    private $filetype = array(); // 需要處理的文件類(lèi)型
    // 初始化
    public function __construct($filetype=array()){
    if($filetype){
    $this->filetype = $filetype;
    }
    }
    /** 重寫(xiě)FindFile handle方法
    * @param String $file 文件路徑
    */
    protected function handle($file){
    if($this->check_ext($file) && $this->check_utf8bom($file)){ // utf8+bom
    $this->clear_utf8bom($file); // clear
    array_push($this->files, $file); // save log
    }
    }
    /** 檢查文件是否utf8+bom
    * @param String $file 文件路徑
    * @return boolean
    */
    private function check_utf8bom($file){
    $content = file_get_contents($file);
    return ord(substr($content,0,1))===0xEF && ord(substr($content,1,1))===0xBB && ord(substr($content,2,1))===0xBF;
    }
    /** 清除utf8+bom
    * @param String $file 文件路徑
    */
    private function clear_utf8bom($file){
    $content = file_get_contents($file);
    file_put_contents($file, substr($content,3), true); // 去掉頭三個(gè)字節(jié)
    }
    /** 檢查文件類(lèi)型
    * @param String $file 文件路徑
    * @return boolean
    */
    private function check_ext($file){
    $file_ext = strtolower(array_pop(explode('.',basename($file))));
    if(in_array($file_ext, $this->filetype)){
    return true;
    }else{
    return false;
    }
    }
    }
    ?>
    去除utf8 bom頭Demo遍歷文件示例:
    <?php
    require('FindFile.class.php');
    require('UnsetBom.class.php');
    $folder = dirname(__FILE__);
    $obj = new UnsetBom(array('php','css','js')); // 文件類(lèi)型
    $obj->process($folder);
    print_r($obj->files);
    ?>
    希望本文所述對(duì)大家PHP程序設(shè)計(jì)的學(xué)習(xí)有所幫助。