PHP删除php文件中的BOM头
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2020-12-04 19:18:20
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
把下面文件保存成delbom.php放到网站根目录中访问就可以自动删除文件中的bom头(不会删除文件请放心使用)
set_time_limit(0);
//要检查的后缀
$suffix = array('php', 'html');
if (isset($_GET['dir'])) {
$basedir = $_GET['dir'];
} else {
$basedir = '.';
}
$auto = 1;
checkdir($basedir);
function checkdir($basedir)
{
global $suffix;
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
if (!is_dir($basedir . "/" . $file)) {
$kz = explode('.', $file);
$jc = isset($kz[1]) ? strtolower($kz[1]) : '';
if (!empty($kz) && in_array($jc, $suffix)) {
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.zhaokeli.com>http://www.zhaokeli.com</a></font>");
} else {
return ("<font color=red>BOM found.</font>");
}
} else {
return ("<font color=green>BOM Not Found.</font>");
}
}
function rewrite($filename, $data)
{
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}