PHP清除BOM及输出缓冲处理示例
核心代码实现
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 ("BOM found, automatically removed.");
}else {
return ("BOM found.");
}
}else return ("BOM Not Found.");
}
PHP用于清除意外产生的输出内容,特别是在需要输出图像之前,必须确保之前的所有输出数据都被彻底清空。
只需在调用header函数之前连续使用两个特定的输出控制函数,即可有效解决因前置输出导致的头部错误问题。
ob_get_clean();
ob_clean();
header("Content-type: image/jpeg");
推荐几个实用的BOM检测工具: BOM检测.zip

