PHP批量检测并去除文件BOM头信息代码
如下代码为PHP方式去除当前目录及子目录所有文件BOM信息的代码,新建文件,将其放倒根目录下,然后浏览器访问即可。
例子
将以上代码保存为后缀为php的文件放到需要去除bom的文件目录里面,然后运行该php文件,将会对该目录以及该目录所有的子目录下的文件进行bom检查并去除bom
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8896')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8896><?php
if (isset($_GET['dir'])) { //设置文件目录
$basedir = $_GET['dir'];
} else {
$basedir = '.';
}
$auto = 1;
checkdir($basedir);
function checkdir($basedir)
{
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
if (!is_dir($basedir . "/" . $file)) {
echo "filename: $basedir/$file " . checkBOM("$basedir/$file") . " <br>";
} else {
$dirname = $basedir . "/" . $file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
function checkBOM($filename)
{
global $auto;
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
if ($auto == 1) {
$rest = substr($contents, 3);
rewrite($filename, $rest);
return ("<font color="red">BOM found, automatically removed._<a href="http://www.jiaochengji.com">http://www.jiaochengji.com</a></font>");
} else {
return ("<font color="red">BOM found.</font>");
}
} else
return ("BOM Not Found.");
}
function rewrite($filename, $data)
{
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>
例子二
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1168')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1168><?php
header('content-Type: text/html; charset=utf-8');
$auto=1;/*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/
$basedir='.';
$loop=true;//www.jiaochengji.com echo '当前查找的目录为:'.$basedir.'当前的设置是:';
echo '(1)',$loop?'检查当前目录以及当前目录的子目录':'只针对当前目录进行检测';
echo '(2)',$auto?'检测文件BOM同时去除检测到BOM文件的BOM<br />':'只检测文件BOM不执行去除BOM操作<br />';
checkdir($basedir,$loop);
function checkdir($basedir='',$loop=true){
$basedir=empty($basedir)?'.':$basedir;
if($dh=opendir($basedir)){
while (($file=readdir($dh))!==false){
if($file!='.'&&$file!='..'){
if(!is_dir($basedir.'/'.$file)){
echo '文件: '.$basedir.'/'.$file .checkBOM($basedir.'/'.$file).' <br>';
}else{
if(!$loop) continue;
$dirname=$basedir.'/'.$file;
checkdir($dirname);
}
}
}
closedir($dh);
}
}
function checkBOM($filename){
global $auto;
$contents=file_get_contents($filename);
$charset[1]=substr($contents,0,1);
$charset[2]=substr($contents,1,1);
$charset[3]=substr($contents,2,1);
if(ord($charset[1])==239&&ord($charset[2])==187&&ord($charset[3])==191){
if($auto==1){
$rest=substr($contents,3);
rewrite($filename,$rest);
return (' <font color=red>找到BOM并已自动去除</font>');
}else{
return (' <font color=red>找到BOM</font>');
}
}else{
return (' 没有找到BOM');
}
}
function rewrite($filename,$data){
$filenum=fopen($filename,'w');
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
您可能感兴趣的文章:
PHP 过滤页面中的BOM数据的简单实例
php去掉bom头的代码分享
Php批量去除bom头信息的实现代码
PHP实例:检测并清除文件开头的BOM信息
PHP批量检测并去除文件BOM头信息代码
PHP如何批量检测并去除文件BOM头
php批量删除、清除utf-8文件bom头的代码
检测php文件是否有bom头的代码
批量清除php文件中bom的方法
php中curl实现GET和POST请求抓取网页或上传文件