PHP语言 遍历文件夹及文件类及处理类
小标 2018-07-12 来源 : 阅读 902 评论 0

摘要:本文主要向大家介绍了PHP语言 遍历文件夹及文件类及处理类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言 遍历文件夹及文件类及处理类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

FindFile.class.php
用于遍历目录文件

[php] view plain copy

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

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($file, substr($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

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. ?>  

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程