diff --git a/app/Services/Cron/FreenodeCrawl.php b/app/Services/Cron/FreenodeCrawl.php index 22d88e6c..aff29bb4 100644 --- a/app/Services/Cron/FreenodeCrawl.php +++ b/app/Services/Cron/FreenodeCrawl.php @@ -188,7 +188,8 @@ final class FreenodeCrawl // $sFileContent = base64_decode($sFileContent); // } - file_put_contents($sPath, $sFileContent); +// file_put_contents($sPath, $sFileContent); + self::save_utf8_file($sPath, $sFileContent); self::fileRealUpd($aFrom["id"], $sType, $sFileName); @@ -232,4 +233,24 @@ final class FreenodeCrawl return $sContent; } + private static function save_utf8_file($filename, $content) { + // 自动检测编码 + $encoding = mb_detect_encoding( + $content, + ['UTF-8', 'GBK', 'BIG5', 'ISO-8859-1'], + true + ); + + // 如果不是 UTF-8,就转成 UTF-8 + if ($encoding !== 'UTF-8') { + $content = mb_convert_encoding($content, 'UTF-8', $encoding); + } + + // 确保文件头是 UTF-8(可选) + // $content = "\xEF\xBB\xBF" . $content; // 如果需要 BOM,可以加上 + + return file_put_contents($filename, $content) !== false; + } + + }