zoukankan      html  css  js  c++  java
  • 批量去BOM头 遍历目录及子文件,文件夹 PHP源码

    任意php文件,把最后一行替换成自己的目录 即可

    <?php

    class KillBom
    {
        public static $m_Ext = ['txt', 'php', 'js', 'css'];//检查的扩展名
        /**
         * 传入一个任意文件 ,自动区分定义的扩展名,然后过滤bom
         * @param string $file
         * @return boolean
         */
        public static  function killBomByFile($file)
        {
            $ext = pathinfo($file,PATHINFO_EXTENSION);//获取一个文件 的扩展名
            if (in_array($ext, self::$m_Ext) and  is_file($file))//允许被替换,而且是个文件 (不是目录 )
            {
                $content = file_get_contents($file);//取出文件 详情
             
                if (substr($content, 0, 3) == chr(0xEF) . chr(0xBB) . chr(0xBF))//EFBBBF 检查bom
                {
                    return file_put_contents($file, substr($content, 3)) > 0;//清除bom并写入文件
                }
            }
            return false;
        }
        /**
         * 遍历获取子目录 及文件夹
         * @param string $dir
         * @return string[]
         */
        public static function  getFileListByDir($dir)
        {
            $dir_handle = opendir($dir);//打开文件
            $result = [];//存结果
            while ($file = readdir($dir_handle))//不断读取目录
            {
                if ($file != '.' and $file != '..')//不是本,上级目录
                {
                    $file = $dir . DIRECTORY_SEPARATOR . $file;//组装成文件的绝对路径
                    if (is_dir($file))//是目录 的话
                    {
                        $result = array_merge($result ,  self::getFileListByDir($file));//递归并合并结果
                    } else {
                        $result[] = $file;//记录结果
                    }
                }
            }
            return $result;//返回结果
        }
        /**
         * 清空目录 下所有的bom头文件
         * @param string $dir
         */
        public static function killDir($dir)
        {
            $files = self::getFileListByDir($dir);//先找到所有文件
            foreach ($files as $file)//遍历
            {
                if (!self::killBomByFile($file))//干掉
                {
                    echo $file .' -> no bom! <br>'.chr(13);//结果
                } else {
                    echo $file . ' -> bom is killed! <br>'.chr(13);//结果
                }
            }
             
        }
    }
    //把下面这行替换成自己的目录
    KillBom::killDir('您的目录');
  • 相关阅读:
    Vue 踩坑-2 vue文件中style的scoped属性
    IIS发布Vue项目F5刷新404问题
    .NET Core 3.1 + Hangfire 配置以及踩坑
    Vue 踩坑-1-跨域问题
    Docker 部署VUE项目
    (转)如何利用EnteLib Unity Interception Extension 和PIAB实现Transaction的Call Handler
    Unity 中的策略注入(转)
    面向方面的编程、侦听和 Unity 2.0(转)
    Unity 中的拦截功能(转)
    [转]推荐分享22个优秀的项目管理与协作工具
  • 原文地址:https://www.cnblogs.com/ghjbk/p/6688824.html
Copyright © 2011-2022 走看看