首页 后端开发 php教程 PHP 遍历文件夹及文件类及处理类

PHP 遍历文件夹及文件类及处理类

Feb 18, 2017 am 09:28 AM

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;
        }
    }

}
?>
登录后复制


Demo unset utf8 bom

<?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中文网(www.php.cn)!


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

11个最佳PHP URL缩短脚本(免费和高级) 11个最佳PHP URL缩短脚本(免费和高级) Mar 03, 2025 am 10:49 AM

11个最佳PHP URL缩短脚本(免费和高级)

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

在Laravel中使用Flash会话数据

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

简化的HTTP响应在Laravel测试中模拟了

Instagram API简介 Instagram API简介 Mar 02, 2025 am 09:32 AM

Instagram API简介

构建具有Laravel后端的React应用程序:第2部分,React 构建具有Laravel后端的React应用程序:第2部分,React Mar 04, 2025 am 09:33 AM

构建具有Laravel后端的React应用程序:第2部分,React

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

php中的卷曲:如何在REST API中使用PHP卷曲扩展

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

在Codecanyon上的12个最佳PHP聊天脚本

宣布 2025 年 PHP 形势调查 宣布 2025 年 PHP 形势调查 Mar 03, 2025 pm 04:20 PM

宣布 2025 年 PHP 形势调查

See all articles