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 || $depthmaxdepth){ // 判断深度

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频道!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值