PHP批量去除文件头部Bom信息
来源:互联网 发布:mysql书籍推荐 知乎 编辑:程序博客网 时间:2024/05/15 20:50
在linux环境下开发的网站,要移植到win2008+IIS7上布署,验证码图片在windows下始终显示不出来,linux下显示是正常的,查其原因,应该是加载的文件里头部带了bom信息,导致显示不出来,于是想到了写个批量替换文件头部bom信息。分享给大家~
什么是文件头部Bom? 不知道的童鞋自补一下:查看文章
说白了,就是在保存文件的时候,文件前面会多出一串隐藏的字符,但网站文件那么多,我们手工来重新编辑太麻烦,用下面的程序来批量去除文件头部Bom:
调用方法二:
http://你的域名/bom.php?auto=1&dir=./test/&r=1
什么是文件头部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
0 0
- PHP批量去除文件头部Bom信息
- 使用PHP批量去除文件UTF8 BOM信息
- Php批量去除bom工具源代码
- php制作bom批量去除工具
- Php批量去除bom工具源代码
- php 批量去除bom头方法
- php批量去除bom的代码
- PHP批量检测并去除文件BOM头代码实例 导致空白行解决办法
- 通过Python脚本批量去除utf8的bom头信息
- PHP去除BOM头
- PHP去除BOM头
- php 去除 BOM头
- Php去除Bom
- PHP去除BOM头
- php 去除bom 类
- PHP去除BOM头
- 去除BOM php代码
- PHP去除BOM头
- Js遍历键值对形式对象或Map形式
- 欢迎使用CSDN-markdown编辑器
- JS原型链问题
- LVS:三种负载均衡方式比较
- thinkphp跳转和重定向
- PHP批量去除文件头部Bom信息
- RAMAnimatedTabBarController -swift kit
- visual studio运行时库MT、MTd、MD、MDd的研究
- ueditor编辑器使用
- The method getTextContent() is undefined for the type Element
- 08-1. 求一批整数中出现最多的个位数字(20)
- listview添加头部view问题,数组越界问题
- thinkphp的I方法
- Template Method Pattern