PHP批量去除文件头部Bom信息

本文介绍了一种批量去除文件头部BOM信息的方法,适用于网站从Linux环境迁移到Windows环境时遇到的验证码图片显示问题。通过提供的PHP脚本,可以自动检测并移除文件头部的BOM标记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在linux环境下开发的网站,要移植到win2008+IIS7上布署,验证码图片在windows下始终显示不出来,linux下显示是正常的,查其原因,应该是加载的文件里头部带了bom信息,导致显示不出来,于是想到了写个批量替换文件头部bom信息。分享给大家~

什么是文件头部Bom?  不知道的童鞋自补一下: 查看文章
说白了,就是在保存文件的时候,文件前面会多出一串隐藏的字符,但网站文件那么多,我们手工来重新编辑太麻烦,用下面的程序来批量去除文件头部Bom:

文件名:bom.php

<?php
/**
 * 批量去除文件头bom.
 * Author: Simon
 * E-mail: vsiryxm@qq.com
 * Date: 2015-8-5
 */

class Bom {
    static public $total = 0; //文件数统计
    static public $count = 0; //替换数统计

    protected $config = array(
        'auto' => 1,    // 是否自动替换 1为自动替换
        'dir'  => '.',  // 遍历的目录 默认当前
        'r'    => 1,    // 1为递归
    );

    function __construct(){
        if(isset($_REQUEST['auto'])){
            $this->config['auto'] = $_REQUEST['auto'];
        }
        if(!empty($_REQUEST['dir'])){
            $this->config['dir'] = $_REQUEST['dir'];
        }
        if(isset($_REQUEST['r'])){
            $this->config['r'] = $_REQUEST['r'];
        }
    }

    // 设置
    public function set($key,$value=''){
        if(isset($this->config[$key])){
            $this->config[$key] = $value;
        }
    }

    // 遍历目录下的文件并替换bom
    public function remove($curr_dir=''){
        $dir = !empty($curr_dir)?$curr_dir:$this->config['dir'];
        if($files = opendir($dir)) {
            ob_end_flush(); // 直接输出缓冲区内容
            while(($file = readdir($files)) !== false) {
                if($file != '.' && $file != '..'){
                    // 是目录 递归
                    if(is_dir($dir.DIRECTORY_SEPARATOR.$file) && $this->config['r']==1){
                        $this->remove($dir.DIRECTORY_SEPARATOR.$file);
                    }
                    elseif(!is_dir($dir.DIRECTORY_SEPARATOR.$file)){
                        self::$total++;
                        if($content = $this->checkBOM($dir.DIRECTORY_SEPARATOR.$file)){
                            if ($this->config['auto']==1){
                                $content = substr($content, 3);
                                $this->rewrite($dir.DIRECTORY_SEPARATOR.$file,$content);
                                echo '<span style=\'color:red\'>'.$dir.DIRECTORY_SEPARATOR.$file.' 已经替换!</span><br>'.PHP_EOL;
                                self::$count++;
                            }
                            else{
                                echo '<span style=\'color:red\'>'.$dir.DIRECTORY_SEPARATOR.$file.' 存在Bom!</span><br>'.PHP_EOL;
                            }
                        }
                        else{
                            echo $dir.DIRECTORY_SEPARATOR.$file.' 没有Bom!<br>'.PHP_EOL;
                        }
                    }
                }
                flush();
                //sleep(1);
            }
            closedir($files);
        }
        else{
            echo '检查路径不存在!';
        }
    }

    // 检查Bom
    public function checkBOM($filename){
        $content = file_get_contents($filename);
        if(!empty($content)){
            $charset[1] = substr($content, 0, 1);
            $charset[2] = substr($content, 1, 1);
            $charset[3] = substr($content, 2, 1);
            if (ord($charset[1])==239 && ord($charset[2])==187 && ord($charset[3])==191){
                return $content;
            }
        }
        return false;
    }

    // 重写文件
    public function rewrite($filename, $data){
        $file = fopen($filename, "w");
        flock($file, LOCK_EX);
        fwrite($file, $data);
        fclose($file);
    }

}

////////////////////////////////////////////////
//调用
$bom = new Bom();

echo <<<EOF
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta charset="UTF-8" />
<title></title>
<meta name="author" content="Simon(QQ42564096)" />
<style>
body,h1,div{margin:0;padding:0;font:14px/1.5 'Microsoft Yahei',tahoma,arial;}
.process {width:800px;height:750px;padding:20px;border:1px solid #ddd;overflow:scroll;margin-left:20px;line-height:180%;}
h1{font-size:25px;text-indent:20px;margin:20px 0 10px 0;}
</style>
</head>
<body>
<h1 id='result'>开始检查Bom...</h1>
<div class='process'>
EOF;

$bom->remove(); 

echo '<script>document.getElementById(\'result\').innerHTML = \'检测完毕!共有'.Bom::$total.'个文件,替换了'.Bom::$count.'个文件\';</script>';
echo <<<EOF
</div>
</body>
</html>
EOF;
$bom = null;
?>
从上面的类可以看出,我们在调用时,使用默认参数(当前目录、可以递归、自动移除)来运行程序,也可以设置参数来运行:

调用方法二:

//调用
$bom = new Bom();
$bom->set('auto',0);       //不自动替换,只检查
$bom->set('dir','./test');  //当前目录下的test目录
$bom->set('r',0);            //不递归查找子目录
调用方法三:
http://你的域名/bom.php?auto=1&dir=./test/&r=1

运行效果:




附批量去除文件头部Bom信息文件包>>>
上传到网站任意目录下,在浏览器里访问运行即可。在运行前请先备份好站点文件,保证要替换的文件可写入。
点击下载:

bom.class.zip


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值