检查php文件中是否含有bom的函数

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?php
/*检测并清除BOM*/
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);
}
}
}//end while
closedir($dh);
}//end if($dh
}//end function
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.</font>";
}else{
return ("<font color=red>BOM found.</font>");
}
} www.jb51.net
else return ("BOM Not Found.");
}//end function
function rewrite($filename, $data){
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}//end function
?>

相关文章

php求今天、昨天、明天时间戳的简单实现方法

本文实例讲述了php求今天、昨天、明天时间戳的简单实现方法。分享给大家供大家参考,具体如下: echo strtotime('now'),'<br>';//现在 echo...

php中取得URL的根域名的代码

复制代码 代码如下: <?php /** * 取得根域名 * * @author lonely * @create 2011-3-11 * @version 0.1 * @last...

php检索或者复制远程文件的方法

本文实例讲述了php检索或者复制远程文件的方法。分享给大家供大家参考。具体实现方法如下: <?php if(!@copy('http://someserver.com/...

PHP解决中文乱码

在php中,中文乱码非常头疼,很麻烦,所以根据在编程的经验,总结以下方法(以utf_8为例), 1.php中在头部header设置编码方式 header("Content-type:te...

PHP封装的数据库保存session功能类

本文实例讲述了PHP封装的数据库保存session功能类。分享给大家供大家参考,具体如下: PHP用数据库保存session类: <?php class SafeSes...