PHP 遍历文件夹及文件类及处理类

来源:互联网 发布:美橙互联域名过户 编辑:程序博客网 时间:2024/06/03 19:06
FindFile.class.php
用于遍历目录文件
[php] view plain copy
print?
  1. <?php  
  2. /** 遍历文件夹及文件类 
  3. *   Date:   2013-03-21 
  4. *   Author: fdipzone 
  5. *   Ver:    1.0 
  6. */  
  7. class FindFile{  
  8.   
  9.     public $files = array();    // 存储遍历的文件  
  10.     protected $maxdepth;        // 搜寻深度,0表示没有限制  
  11.   
  12.   
  13.     /*  遍历文件及文件夹 
  14.     *   @param String $spath     文件夹路径 
  15.     *   @param int    $maxdepth  搜寻深度,默认搜寻全部 
  16.     */  
  17.     public function process($spath$maxdepth=0){  
  18.         if(isset($maxdepth) && is_numeric($maxdepth) && $maxdepth>0){  
  19.             $this->maxdepth = $maxdepth;  
  20.         }else{  
  21.             $this->maxdepth = 0;  
  22.         }  
  23.         $this->files = array();  
  24.         $this->traversing($spath); // 遍历  
  25.     }  
  26.   
  27.   
  28.     /*  遍历文件及文件夹 
  29.     *   @param String $spath 文件夹路径 
  30.     *   @param int    $depth 当前文件夹深度 
  31.     */  
  32.     private function traversing($spath$depth=1){  
  33.         if($handle = opendir($spath)){  
  34.             while(($file=readdir($handle))!==false){  
  35.                 if($file!='.' && $file!='..'){  
  36.                     $curfile = $spath.'/'.$file;  
  37.   
  38.                     if(is_dir($curfile)){ // dir  
  39.                         if($this->maxdepth==0 || $depth<$this->maxdepth){ // 判断深度  
  40.                             $this->traversing($curfile$depth+1);  
  41.                         }  
  42.                     }else{  // file  
  43.                         $this->handle($curfile);  
  44.                     }  
  45.   
  46.                 }  
  47.             }  
  48.             closedir($handle);  
  49.         }  
  50.     }  
  51.   
  52.   
  53.     /** 处理文件方法 
  54.     *  @param String $file 文件路径 
  55.     */  
  56.     protected function handle($file){  
  57.         array_push($this->files, $file);  
  58.     }  
  59.   
  60. }  
  61. ?>  

UnsetBom.class.php用于清除utf8+bom文件的bom,即头三个字节 0xEF 0xBB 0xBF,继承FindFile类
[php] view plain copy
print?
  1. <?php  
  2. /** 遍历所有文件,清除utf8+bom 0xEF 0xBB 0xBF 
  3. *   Date:   2013-03-21 
  4. *   Author: fdipzone 
  5. *   Ver:    1.0 
  6. */  
  7. class UnsetBom extends FindFile{  
  8.   
  9.   
  10.     private $filetype = array(); // 需要处理的文件类型  
  11.   
  12.   
  13.     // 初始化  
  14.     public function __construct($filetype=array()){  
  15.         if($filetype){  
  16.             $this->filetype = $filetype;  
  17.         }  
  18.     }  
  19.   
  20.   
  21.     /** 重写FindFile handle方法 
  22.     *   @param  String $file 文件路径 
  23.     */  
  24.     protected function handle($file){  
  25.         if($this->check_ext($file) && $this->check_utf8bom($file)){ // utf8+bom  
  26.             $this->clear_utf8bom($file);        // clear  
  27.             array_push($this->files, $file);    // save log  
  28.         }  
  29.     }  
  30.   
  31.   
  32.     /** 检查文件是否utf8+bom 
  33.     *   @param  String $file 文件路径 
  34.     *   @return boolean 
  35.     */  
  36.     private function check_utf8bom($file){  
  37.         $content = file_get_contents($file);  
  38.         return ord(substr($content,0,1))===0xEF && ord(substr($content,1,1))===0xBB && ord(substr($content,2,1))===0xBF;  
  39.     }  
  40.   
  41.   
  42.     /** 清除utf8+bom 
  43.     *   @param String $file 文件路径 
  44.     */  
  45.     private function clear_utf8bom($file){  
  46.         $content = file_get_contents($file);  
  47.         file_put_contents($filesubstr($content,3), true); // 去掉头三个字节  
  48.     }  
  49.   
  50.   
  51.     /** 检查文件类型 
  52.     *   @param  String $file 文件路径 
  53.     *   @return boolean 
  54.     */  
  55.     private function check_ext($file){  
  56.         $file_ext = strtolower(array_pop(explode('.',basename($file))));  
  57.         if(in_array($file_ext$this->filetype)){  
  58.             return true;  
  59.         }else{  
  60.             return false;  
  61.         }  
  62.     }  
  63.   
  64. }  
  65. ?>  

Demo unset utf8 bom
[php] view plain copy
print?
  1. <?php  
  2. require('FindFile.class.php');  
  3. require('UnsetBom.class.php');  
  4.   
  5. $folder = dirname(__FILE__);  
  6.   
  7. $obj = new UnsetBom(array('php','css','js')); // 文件类型  
  8. $obj->process($folder);  
  9.   
  10. print_r($obj->files);  
  11. ?>