- 從linux服務器nginx上把一個網站遷移到windows的IIS上
數據什么的都么有問題,配置好rewrite以后,訪問網站,發現樣式變動了,網站上方空出了一塊
我用chrome瀏覽器的審查元素一看,發現head里的內容全到Body里了,而且body的最開始出多出了一塊帶引號的空白!
但是如果右鍵查看源代碼的話,代碼是正確的,沒有問題!
正常狀態

錯誤狀態
求大牛解救
問題原因:
網站采用了UTF-8無BOM編碼,但是在使用include或者require包含文件的時候,包含了一個UTF-8有BOM的文件,就產生上述現象。
解決方法:
對網站所有文件進行去BOM操作
clearBOM.php(放到根目錄下執行,執行前先備份網站,以備不測)
01 |
<?php |
02 |
$basedir = str_replace('/clearBOM.php','',str_replace('\\','/',dirname(__FILE__))); |
03 |
$auto = 1; |
04 |
checkdir($basedir); |
05 |
function checkdir($basedir){ |
06 |
if ($dh = opendir($basedir)) { |
07 |
while (($file = readdir($dh)) !== false) { |
08 |
if ($file != '.' && $file != '..'){ |
09 |
if (!is_dir($basedir.'/'.$file)) { |
10 |
$filename = $basedir.'/'.$file; |
11 |
echo 'filename:'.$basedir.'/'.$file.checkBOM($filename).'<br>'; |
12 |
} else { |
13 |
$dirname = $basedir.'/'.$file; |
14 |
checkdir($dirname); |
15 |
} |
16 |
} |
17 |
} |
18 |
closedir($dh); |
19 |
} |
20 |
} |
21 |
22 |
function checkBOM ($filename) { |
23 |
global $auto; |
24 |
$contents = file_get_contents($filename); |
25 |
$charset[1] = substr($contents, 0, 1); |
26 |
$charset[2] = substr($contents, 1, 1); |
27 |
$charset[3] = substr($contents, 2, 1); |
28 |
if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) { |
29 |
if ($auto == 1) { |
30 |
$rest = substr($contents, 3); |
31 |
rewrite ($filename, $rest); |
32 |
return '<font color=red>BOM found,automatically removed.</font>'; |
33 |
} else { |
34 |
return '<font color=red>BOM found.</font>'; |
35 |
} |
36 |
} else { |
37 |
return 'BOM Not Found.'; |
38 |
} |
39 |
} |
40 |
41 |
function rewrite ($filename, $data) { |
42 |
$filenum = fopen($filename, 'w'); |
43 |
flock($filenum, LOCK_EX); |
44 |
fwrite($filenum, $data); |
45 |
fclose($filenum); |
46 |
} |
47 |
?> |
