有时候,在php网站迁移或者安装结束后,会发现某些页面显示空白,某些验证码图片显示不正常。通常这种情况很有可能是因为使用记事本修改网站配置文件,导致相应文件被记事本添加了BOM头,这是三个二进制为0xEF 0xBB 0xBF的文件标记。 对这种情况,一般有两种方法处理。
使用专业代码编辑器【如notepad++】修改配置文件,并保存为utf-8无bom格式。
使用自动脚本自动清理。只需要把以下bom.php上传到网站根目录,并访问http://域名/bom.php 就可以自动清理相应bom,清理完毕后,记得删除相应文件即可。
//remove the utf-8 boms
header("Content-type: text/html; charset=utf-8");
ini_set('memory_limit','100M');
if (isset($_GET['dir'])){ //config the basedir
$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 "文件名: $basedir/$file ".checkBOM("$basedir/$file")."
";
}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 ("已经自动移除PHP文件BOM");
} else {
return ("文件含BOM");
}
}
else return ("文件无BOM");
}
function rewrite ($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
本网站采用BY-NC-SA 4.0协议进行授权 | 转载请注明原文链接:https://www.disidu.com/post/4.html
如果觉得本文对您有帮助或者您心情好~可以微信打赏支持一下本站:↓↓↓