PHP遍历文件夹与文件类及处理类用法实例


Posted in PHP onSeptember 23, 2014

本文实例讲述了PHP遍历文件夹与文件类及处理类用法,非常具有实用价值。分享给大家供大家参考。具体方法如下:

FindFile.class.php类文件用于遍历目录文件,具体代码如下:

<?php 
/** 遍历文件夹及文件类 
*  Date:  2013-03-21 
*  Author: fdipzone 
*  Ver:  1.0 
*/ 
class FindFile{ 
 
  public $files = array();  // 存储遍历的文件 
  protected $maxdepth;    // 搜寻深度,0表示没有限制 
 
  /* 遍历文件及文件夹 
  *  @param String $spath   文件夹路径 
  *  @param int  $maxdepth 搜寻深度,默认搜寻全部 
  */ 
  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 当前文件夹深度 
  */ 
  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,即头三个字节 0xEF 0xBB 0xBF,继承FindFile类,具体代码如下:

<?php 
/** 遍历所有文件,清除utf8+bom 0xEF 0xBB 0xBF 
*  Date:  2013-03-21 
*  Author: fdipzone 
*  Ver:  1.0 
*/ 
class UnsetBom extends FindFile{ 
 
  private $filetype = array(); // 需要处理的文件类型 
 
  // 初始化 
  public function __construct($filetype=array()){ 
    if($filetype){ 
      $this->filetype = $filetype; 
    } 
  } 
 
  /** 重写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); // 去掉头三个字节 
  } 
 
  /** 检查文件类型 
  *  @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')); // 文件类型 
$obj->process($folder); 
 
print_r($obj->files); 
?>

希望本文所述对大家PHP程序设计的学习有所帮助。

PHP 相关文章推荐
多重?l件?合查?(一)
Oct 09 PHP
PHP __autoload()方法真的影响性能吗?
Mar 30 PHP
php随机输出名人名言的代码
Oct 07 PHP
用Json实现PHP与JavaScript间数据交换的方法详解
Jun 20 PHP
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
Jan 22 PHP
自己写的兼容低于PHP 5.5版本的array_column()函数
Oct 24 PHP
php计算数组相同值出现次数的代码(array_count_values)
Jan 20 PHP
PHP动态规划解决0-1背包问题实例分析
Mar 23 PHP
PHP中SSO Cookie登录分析和实现
Nov 06 PHP
[原创]smarty简单模板变量输出方法
Jul 09 PHP
PHP结合Ffmpeg快速搭建流媒体服务的实践记录
Oct 31 PHP
laravel框架中视图的基本使用方法分析
Nov 23 PHP
PHP邮件发送类PHPMailer用法实例详解
Sep 22 #PHP
php实现的CSS更新类实例
Sep 22 #PHP
php的XML文件解释类应用实例
Sep 22 #PHP
php实现的返回数据格式化类实例
Sep 22 #PHP
php实现的替换敏感字符串类实例
Sep 22 #PHP
php实现的发送带附件邮件类实例
Sep 22 #PHP
PHP实现AES256加密算法实例
Sep 22 #PHP
You might like
PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数的小问题
2011/05/29 PHP
PHP 八种基本的数据类型小结
2011/06/01 PHP
PHP中使用imagick生成PSD文件缩略图教程
2015/01/26 PHP
php返回相对时间(如:20分钟前,3天前)的方法
2015/04/14 PHP
使用PHPStorm+XDebug搭建单步调试环境
2017/11/19 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
强悍无比的WEB开发好助手FireBug(Firefox Plugin)
2007/01/16 Javascript
AJAX异步从优酷专辑中采集所有视频及信息(JavaScript代码)
2010/11/20 Javascript
TBCompressor js代码压缩
2011/01/05 Javascript
JavaScript实现找出字符串中第一个不重复的字符
2014/09/03 Javascript
Bootstrap每天必学之简单入门
2015/11/19 Javascript
基于ajax与msmq技术的消息推送功能实现代码
2016/12/26 Javascript
JS自定义滚动条效果简单实现代码
2020/10/27 Javascript
微信小程序之蓝牙的链接
2017/09/26 Javascript
使用vue点击li,获取当前点击li父辈元素的属性值方法
2018/09/12 Javascript
微信小程序网络层封装的实现(promise, 登录锁)
2019/05/08 Javascript
vue 解决路由只变化参数页面组件不更新问题
2019/11/05 Javascript
Python Mysql自动备份脚本
2008/07/14 Python
python获取局域网占带宽最大3个ip的方法
2015/07/09 Python
解决python3在anaconda下安装caffe失败的问题
2017/06/15 Python
Pandas Shift函数的基础入门学习笔记
2018/11/16 Python
Python 加密与解密小结
2018/12/06 Python
python修改txt文件中的某一项方法
2018/12/29 Python
Python对称的二叉树多种思路实现方法
2020/02/28 Python
详解CSS3中字体平滑处理和抗锯齿渲染
2017/03/29 HTML / CSS
华为俄罗斯官方网上商城:购买Huawei手机和平板
2017/04/21 全球购物
锐步美国官方网站:Reebok美国
2018/01/10 全球购物
Booking.com缤客中国:全球酒店在线预订网站
2020/05/03 全球购物
农村婚礼证婚词
2014/01/08 职场文书
美术教师岗位职责
2014/03/18 职场文书
精彩广告词大全
2014/03/19 职场文书
学校督导评估方案
2014/06/10 职场文书
社会治安综合治理责任书
2015/01/29 职场文书
运动会报道稿大全
2015/07/23 职场文书
爱岗敬业先进典型事迹材料(2016推荐版)
2016/02/26 职场文书
职场:企业印章管理制度(模板)
2019/10/18 职场文书