php輸出亂碼的(de)問(wèn)題

就(jiù)是(shì)文(wén)件(jiàn)BOM導緻的(de),所有(yǒu)引用•↔™∏(yòng)的(de)文(wén)件(jiàn)包含BOM均會(≥÷‌huì)導緻這(zhè)樣的(de)問(wèn)題。解決方案₽ "如(rú)下(xià)

将以下(xià)代碼在根目錄新建個(gè)文(wén)件(j∏₽☆€iàn) 并運行(xíng)一(yī)下(xià) 問(wèn) ™α題完美(měi)解決

<?php
if (isset($_GET['dir'])) { //設置文(wén♦♠β)件(jiàn)目錄 
    $basedir = $_GET['dir']∞☆♠;
} else {
    $basedir = '.';
}

$auto = 1;
checσ'kdir($basedir);

function ch​¥εeckdir($basedir)
{
    if (₽™ $dh = opendir($basedir)) {
        w✘£★hile (($file = readdir($dh)) ×∏ α!== false) {
            if σ ($file != '.' && $file != '..') {
   ≤               if (!is_dir($basedir . "/&←Ω quot; . $file)) {
                    echo✔£ "filename: $basedir/$file " .₩‌δ≤ checkBOM("$basedir/$f‍↓ε>ile") . " <br>";
∏£>                } else {
       ≤☆             $dirname = $basedir . &qu•✔ot;/" . $file;
                "<™     checkdir($dirname);
                }
     '♣       }
        }
        closedir($dδ∞h);
    }
}
function checkBOM($filename)
{
¶$✔≠    global $auto;
    $content♠&s   = file_get_contents($filename);
 ±    $charset[1] = substr($contents,™↑  0, 1);
    $charset[2] = substr($c∏&ontents, 1, 1);
    $charset[3] = substr(×✔$contents, 2, 1);
    if (∑'"ord($charset[1]) == 239 &&am‌δδ p; ord($charset[2]) == 187 &↔ ♦>;& ord($charset[3]) == 191) {
    ε↓↓↑    if ($auto == 1) {
            $rest = subs'¥​§tr($contents, 3);
            rewrite($filen§±←ame, $rest);
            retur₹♠±★n ('<font color="red">BOM f←α☆₽ound, automatically removed.</font&©∏♣™gt;'); 
        } else {
       £πγ      return ('<font color=♠÷§♦"red">BOM found.&lπ©t;/font>');
        }
    } else
 →®©       return ("BOM Nα>ot Found.");
}

function rewrite($filename,÷≥←¶ $data)
{
    $filenum = fopen($filenam₹☆δ₩e, "w");
    flock($filenum, LOCK_E↑γX);
    fwrite($filenum, $data);
   ​π→× fclose($filenum);
}
?>

發表回複

您的(de)郵箱地(dì)址不(bù)會(huì)被公開(kāi)。 必填項已用(yòng) * 标注