




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、php遍历文件夹与文件类及处理类用法实例_ 本文实例讲解并描述了php遍历文件夹与文件类及处理类用法,特别具有有用价值。分享给大家供大家参考。具体方法如下: findfile.class.php类文件用于遍历名目文件,具体代码如下: ?php /* 遍历文件夹及文件类 * date: 2021-03-21 * author: fdipzone * ver: 1.0 */ class findfile public $files = array(); / 存储遍历的文件 protected $maxdepth; / 搜寻深度,0表示没有限制 /* 遍历文件及文件夹 * param string
2、$spath 文件夹路径 * param int $maxdepth 搜寻深度,默认搜寻全部 */ public function process($spath, $maxdepth=0) if(isset($maxdepth) is_numeric($maxdepth) $maxdepth0) $this-maxdepth = $maxdepth; else $this-maxdepth = 0; $this-files = array(); $this-traversing($spath); / 遍历 /* 遍历文件及文件夹 * param string $spath 文件夹路径 * pa
3、ram 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)
4、; 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: 2021-03-2
5、1 * 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
6、_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)=
7、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 = n
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 学校游泳馆管理制度
- 学校营养政管理制度
- 学生上学队管理制度
- 学生用手机管理制度
- 宁洱县财务管理制度
- 安全生物柜管理制度
- 安环部综合管理制度
- 安防部工作管理制度
- 实行平安卡管理制度
- 宠物火化店管理制度
- 2025年高考河北卷物理高考真题+解析(参考版)
- 2025年河南郑州中原绿色产业生态发展公司招聘笔试参考题库含答案解析
- 西南林业大学《算法分析与设计》2023-2024学年第二学期期末试卷
- 2025民用无人机驾驶员合格审定规则
- 夏令营笔试题及答案保研
- DB43-T 2036-2021 山银花采收与产地初加工技术规程
- 极低或超低出生体重儿经导管动脉导管未闭封堵术专家共识(2025)解读
- 防出轨婚前协议书
- 生态康养小镇建设项目可行性研究报告
- 挖掘机考试试题及答案
- 年中国鹦鹉养殖市场发展策略及投资潜力可行性预测报告
评论
0/150
提交评论