PHP遍历文件夹与文件类及处理类用法实例
2015-01-24信息快讯网
这篇文章主要介绍了PHP遍历文件夹与文件类及处理类用法实例,包括了文件及文件夹的遍历以及清除utf8的bom头方法,非常实用,需要的朋友可以参考下
本文实例讲述了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使用正则表达式获取图片url的方法
php使用CURL伪造IP和来源实例详解
php+mysql实现无限分类实例详解
php截取html字符串及自动补全html标签的方法
php在linux下检测mysql同步状态的方法
php导入excel文件到mysql数据库的方法
PHP上传文件时自动分配路径的方法
PHP中使用SimpleXML检查XML文件结构实例
php静态文件返回304技巧分享
php使用fputcsv()函数csv文件读写数据的方法
写一段简单的PHP建立文件夹代码
php使用递归计算文件夹大小
PHP使用glob函数遍历目录或文件夹的方法
thinkphp数据查询和遍历数组实例
php目录遍历函数opendir用法实例
PHP循环遍历数组的3种方法list()、each()和while总结
PHP遍历目录函数opendir()、readdir()、closedir()、rewinddir()总结
PHP实现自动登入google play下载app report的方法
PHP邮件发送类PHPMailer用法实例详解
php实现的CSS更新类实例
php的XML文件解释类应用实例
php实现的返回数据格式化类实例
php实现的替换敏感字符串类实例
php操作csv文件代码实例汇总
PHP+iFrame实现页面无需刷新的异步文件上传
php中使用PHPExcel读写excel(xls)文件的方法
php修改指定文件后缀的方法
php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码
PHP采用自定义函数实现遍历目录下所有文件的方法
php的mkdir()函数创建文件夹比较安全的权限设置方法
PHP数组遍历知识汇总(包含遍历方法、数组指针操作函数、数组遍历测速)
php遍历数组的4种方法总结
CodeIgniter实现更改view文件夹路径的方法
PHP不用递归遍历目录下所有文件的代码
PHP中多维数组的foreach遍历示例
CI框架中libraries,helpers,hooks文件夹详细说明
PHP循环输出指定目录下的所有文件和文件夹路径例子(简单实用)