首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

FCKeditor 圖片上傳中文檔名亂碼完整解決方案(PHP/Java 版)

     編輯:PHP 相關知識

問題根因

FCKeditor 內部使用 UTF-8 編碼,但 Windows 伺服器檔案系統預設為 GBK/Big5,導致中文檔名在上傳、瀏覽、建立資料夾時出現亂碼。

PHP 版解決方案(兩種思路)

方法一:編碼轉換(推薦,保留原檔名)

修改兩個檔案:

  1. commands.php(editor/filemanager/connectors/php/commands.php)
    找到 FileUpload 函數中的 move_uploaded_file():
// 原始
move_uploaded_file($oFile['tmp_name'], $sFilePath);
// 修改為
move_uploaded_file($oFile['tmp_name'], iconv("UTF-8", "GBK", $sFilePath));
  1. util.php(editor/filemanager/connectors/php/util.php)
    找到 ConvertToXmlAttribute 函數:
// 原始
return utf8_encode(htmlspecialchars($value));
// 修改為
return iconv("GBK", "UTF-8", htmlspecialchars($value));

方法二:重新命名檔案(避開編碼問題)

於 commands.php 的 FileUpload 函數中:

// 取得副檔名
$sExtension = strtolower(substr($sFileName, strrpos($sFileName, '.') + 1));
// 產生新檔名:時間戳 + 隨機數 + 副檔名
$sFileName = date("YmdHis") . rand(100, 999) . "." . $sExtension;

Java 版解決方案

1. 上傳 Servlet 設定 Header Encoding

修改 SimpleUploaderServlet.java 與 ConnectorServlet.java:

DiskFileUpload upload = new DiskFileUpload();
upload.setHeaderEncoding("UTF-8"); // 加在這行後

重新編譯並更新 FCKeditor-2.x.jar。

2. Tomcat 伺服器層面設定 URI 編碼

修改 server.xml 的 Connector:

<Connector port="8080" ... URIEncoding="UTF-8" />
<Connector port="8009" ... URIEncoding="UTF-8" />

注意:此設定影響全站,可能干擾其他專案,建議優先用程式碼層面解決。

3. 建立中文資料夾亂碼處理

於 ConnectorServlet 參數取得處轉碼:

String newFolderStr = request.getParameter("NewFolderName");
newFolderStr = new String(newFolderStr.getBytes("ISO-8859-1"), "UTF-8");

通用最佳實踐建議

  • 統一採用方法二(重新命名):以時間戳/UUID 產生純英數檔名,原始中文檔名存資料庫,下載時透過 Content-Disposition 還原。
  • 資料庫欄位儲存:original_name、saved_name、file_path、mime_type、file_size
  • 下載輸出範例:
    header("Content-Type: $mime_type");
    header("Content-Disposition: attachment; filename=\"$original_name\"");
    readfile($file_path);
  • 前端上傳頁面 frmupload.html 加入:<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

其他實用設定

自動增高編輯區

於 fckconfig.js 加入:

FCKConfig.Plugins.Add('autogrow');
FCKConfig.AutoGrowMax = 4000;

確認 editor/plugins/autogrow/ 目錄存在。

刪除圖片清理機制

FCKeditor 無內建刪除功能,建議:

  1. 編輯器 OnBlur 事件收集所有圖片路徑存隱藏欄位
  2. 儲存內容時同步更新資料庫圖片關聯表
  3. 刪除文章時掃描關聯表刪除實體檔案
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved