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

yipeiwu_com6年前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
?>

相关文章

ThinkPHP模板标签eq if 中区分0,null,false的方法

在做项目的时候发现在ThinkPHP的模板标签eq中,对于0、null、false的判断是一样的,也就是说如果变量是这三个值的话就无法区分了,搞的我很郁闷,翻了下官方手册没找到,想想不应...

php5.4以上版本GBK编码下htmlspecialchars输出为空问题解决方法汇总

从旧版升级到php5.4,恐怕最麻烦的就是htmlspecialchars这个问题了!当然,htmlentities也会受影响,不过,对于中文站来说一般用htmlspecialchars...

解析php中用PHPMailer来发送邮件的示例(126.com的例子)

<?phprequire_once('../class.phpmailer.php');$mail= new PHPMailer();$body= "我终于发送邮件成功了!呵呵!g...

SAE实时日志接口SDK用法示例

本文实例讲述了SAE实时日志接口SDK用法。分享给大家供大家参考,具体如下: 新浪SAE是新浪研发中心开发的国内首个公有云平台,从2009年开始到现在也是也来越成熟,开放了很多接口以及服...

php带抄送和密件抄送的邮件发送方法

本文实例讲述了php带抄送和密件抄送的邮件发送方法。分享给大家供大家参考。具体分析如下: 程序中用到了php的mail函数,该函数定义如下: bool mail ( string $t...