运行PHP出现Cannot modify header information
今天安装个UTF-8的程序出现了,Cannot modify header information - headers already sent by错误,搜索了一下解决方法:如果使用utf-8编码,一定要去掉UTF-8中的BOM,这都是因为utf-8编码文件含有的bom原因,而php4,5都是不支持bom的。去掉bom,可以用Notepad++打开转换一下。切记,切记,切记!我的PHP版本估计太低了,呵呵
就是这个问题
网上找的这段代码还真不错,不但能够检测BOM,还能够自动清除BOM,代码如下:
<?php
/*清除rom*/
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 = substr($contents, 0, 1);
$charset = substr($contents, 1, 1);
$charset = substr($contents, 2, 1);
if(ord($charset) == 239 && ord($charset) == 187 && ord($charset) == 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>");
}
}
else return ("BOM Not Found.");
}//end function
function rewrite($filename, $data){
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>
将以上代码保存为PHP文件,放入你要清除BOM的文件的文件夹根目录,运行一次即可
页:
[1]