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

$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类

/** 遍历所有文件,清除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;

}

}

}

?>

Demo unset utf8 bom

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 遍历文件夹及文件类及处理类相关推荐

  1. 微信收藏夹文件保存在服务器安全吗,手机收藏的文件在哪里(微信收藏夹的文件路径)...

    手机微信时常有好友分享的一些有意思的小视频,现在想把这些小视频批量发送到电脑或者分享到网盘上.那么我们首先要清楚微信中的小视频储存的文件夹. 下面我以安卓系统的智能手机为例,进入手机桌面,点击&quo ...

  2. python 删除文件夹和文件

    python 删除文件夹和文件 一.三种删除方法 第一种:删除一个文件夹,无论里面是否有文件或文件夹 (不支持文件,文件夹不存在会报错) 第二种 递归删除dir_path目标文件夹下所有文件,以及各级 ...

  3. python 删除文件夹和文件【转】

    python 删除文件夹和文件 目录 一.三种删除方法 二.删除失败情况 PermissionError: [WinError 5] 拒绝访问 2.1 给python权限 2.2 删除改为更名 一.三 ...

  4. 【Python】1.python 删除文件夹和文件

    目录 1 os+shutil 模块 1.1 删除文件夹 1.2 删除文件夹以及文件 1.3 删除文件下所有内容 1.4 python3相关方法 1.4.1 os.unlink 1.4.2 os.rem ...

  5. 利用java.io.File类实现遍历本地磁盘上指定盘符或文件夹的所有的文件

    2016-11-18 这是本人的第一篇随笔博客,纠结了半天还是选择自己学的时候比较用心的一些知识点上.利用java.io.File类指定本地的文件夹进行遍历所有的文件. package org.lxm ...

  6. C# 第七章『I/O数据流』◆第3节:Directory类、DirectoryInfo类、遍历文件夹

    一.Directory类 Directory 类https://docs.microsoft.com/zh-cn/dotnet/api/system.io.directory?view=netfram ...

  7. VBA遍历文件夹下文件文件实用源码

    '批量遍历文件夹下某类文件,并统计编号 Sub OpenAndClose()Dim MyFile As StringDim s As StringDim count As IntegerMyFile ...

  8. 【python遍历图片进行分类转存到不同的文件夹】

    [python&遍历图片进行分类转存到不同的文件夹] 功能说明 代码实现 知识点补充 Reference: 功能说明 当不同类型的图片存放在同一个文件夹中,方便进行分类,用一个脚本,遍历读取文 ...

  9. python 遍历文件夹和文件

    文件结构如下图 path = r"文件夹1" 遍历1层下的所有文件名和文件夹名 import os fileList = os.listdir(path) # 遍历1层 print ...

最新文章

  1. 从粒子到宇宙的旅行指南
  2. PHP学习:PHP超全局变量(Superglobal)$GLOBALS的介绍以及和global的区别
  3. leetcode 85. 最大矩形(dp)
  4. javascript 总结(常用工具类的封装)(转)
  5. python利用pandas存数据并且展示csv
  6. c/c++教程 - 2.4.2.6 初始化列表语法,初始化列表参数
  7. WCF分布式开发常见错误(3):客户端调用服务出错
  8. python关于组合数据类型_python组合数据类型
  9. 九度OJ 题目1534:数组中第K小的数字(二分解)
  10. Java程序设计24学时轻松掌握_24小时轻松掌握系列——Java程序设计 24学时轻松掌握...
  11. [2018.11.03 T1] 游戏攻略
  12. Google DFP广告管理系统简介:开始与您的网站进行广告集成
  13. 《目标》读后感,一本小说体的管理学著作
  14. 解决笔记本电脑开机速度缓慢的几种措施,亲测有效
  15. imx6ul rtc 驱动修改
  16. java如何返回一个空数组?
  17. 「Jenkins」- No valid crumb was included in request for /ajaxExecutors @20210305
  18. JGG近期专刊征稿汇总|时空组学、人体微生物组、人类遗传病、小麦生物学
  19. 图文识别(一):验证码识别---中、英文、数字混合的单行文字识别等
  20. win10右键 发送到 选项消失解决办法

热门文章

  1. php 清空一个数组_php如何删除一个数组
  2. 一条消息未发,粉丝已破千万
  3. 人大副教授:本科生一定要做科研吗?九大灵魂拷问
  4. 俄国数学家称:“平行线可以相交”,却遭到质疑,死后12年被证实
  5. 这个关系国计民生的“黑盒子” 我们造出来了
  6. 获利上亿!一程序员主犯非法挖矿被抓
  7. 如何用业余时间成为抢手的数据人才?
  8. (pytorch-深度学习系列)pytorch实现多层感知机(自动定义模型)对Fashion-MNIST数据集进行分类-学习笔记
  9. oc 画一个圆弧_用SolidWorks画的塑料衣架,要用到不少让人头疼的曲线
  10. equalsignorecase用法