前一段時間一直受后臺白屏和網頁顯示異常的困擾,發現主要是文件編碼的問題,不僅是UTF和GBK的問題,如果UTF編碼文件多出個BOM也會造成很多不可知的問題。如我前段時間提的靈異現象,今天終于解決了。為了幫助更多朋友解決這個問題,特發一個從網上找的小文件,大家也可以試試,不保行哦。
使用方法是:把以下代碼復制到記事本,保存為liehuo.php,然后傳到你認為編碼有問題的目錄下,運行之。作用是:去除所有UTF-8編碼文件的BOM。(注意:用之前別忘了備份。)
以下為引用的內容: <?php //remove the utf-8 boms //by magicbug at gmail dot com 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 "filename: $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 ("BOM found, automatically removed."); } else { return ("BOM found."); } } else return ("BOM Not Found."); } function rewrite ($filename, $data) { $filenum = fopen($filename, "w"); flock($filenum, LOCK_EX); fwrite($filenum, $data); fclose($filenum); } //liehuo.net |
下一篇 用PHP模擬登陸