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 相关文章推荐
PHP输入流php://input介绍
Sep 18 PHP
探讨各种PHP字符串函数的总结分析
Jun 05 PHP
php的array数组和使用实例简明教程(容易理解)
Mar 20 PHP
PHP exif扩展方法开启详解
Jul 28 PHP
ci检测是ajax还是页面post提交数据的方法
Nov 10 PHP
smarty模板引擎中自定义函数的方法
Jan 22 PHP
PHP封装CURL扩展类实例
Jul 28 PHP
php生成二维码图片方法汇总
Dec 17 PHP
PHP删除二维数组中相同元素及数组重复值的方法示例
May 05 PHP
[原创]php token使用与验证示例【测试可用】
Aug 30 PHP
php strftime函数获取日期时间(switch用法)
May 16 PHP
Laravel框架控制器的middleware中间件用法分析
Sep 30 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防攻击代码升级版
2010/12/29 PHP
PHP中输出转义JavaScript代码的实现代码
2011/04/22 PHP
探讨php中遍历二维数组的几种方法详解
2013/06/08 PHP
PHP序列化/对象注入漏洞分析
2016/04/18 PHP
php app支付宝回调(异步通知)详解
2018/07/25 PHP
PHP的重载使用魔术方法代码实例详解
2021/02/26 PHP
收集的一些Array及String原型对象的扩展实现代码
2010/12/05 Javascript
js函数模拟显示桌面.scf程序示例
2014/04/20 Javascript
常用的JavaScript模板引擎介绍
2015/02/28 Javascript
基于Jquery+div+css实现弹出登录窗口(代码超简单)
2015/10/27 Javascript
用JS实现轮播图效果(二)
2016/06/26 Javascript
jQuery事件委托之Safari
2016/07/05 Javascript
EasyUI 结合JS导出Excel文件的实现方法
2016/11/10 Javascript
原生js实现淘宝购物车功能
2020/06/23 Javascript
Bootstrap table学习笔记(2) 前后端分页模糊查询
2017/05/18 Javascript
安装vue-cli报错 -4058 的解决方法
2017/10/19 Javascript
JavaScript代码执行的先后顺序问题
2017/10/29 Javascript
简述Angular 5 快速入门
2017/11/04 Javascript
Router解决跨模块下的页面跳转示例
2018/01/11 Javascript
ES6 Set结构的应用实例分析
2019/06/26 Javascript
vue中改变滚动条样式的方法
2020/03/03 Javascript
jQuery实现html可联动的百分比进度条
2020/03/26 jQuery
[41:56]Spirit vs Liquid Supermajor小组赛A组 BO3 第一场 6.2
2018/06/03 DOTA
python去除所有html标签的方法
2015/05/05 Python
Python将多份excel表格整理成一份表格
2018/01/03 Python
Python语言的变量认识及操作方法
2018/02/11 Python
在IPython中执行Python程序文件的示例
2018/11/01 Python
python代码实现将列表中重复元素之间的内容全部滤除
2020/05/22 Python
python里的单引号和双引号的有什么作用
2020/06/17 Python
css3绘制天猫logo实现代码
2012/11/06 HTML / CSS
canvas 绘图时位置偏离的问题解决
2020/09/16 HTML / CSS
智能旅行箱:Horizn Studios
2018/04/30 全球购物
介绍一下gcc特性
2015/10/31 面试题
禁毒宣传标语
2014/06/19 职场文书
员工辞职信范文大全
2015/05/12 职场文书
导游词之山海关
2019/12/10 职场文书