本文詳細記錄 CKEditor 4 與 CKFinder 3 在 PHP 環境下的安裝、配置、權限控制、上傳路徑動態切換、中文文件名重命名等完整流程。
// config.js
config.language = 'zh-cn';
config.skin = 'moono-lisa';
config.uiColor = '#F7F7F7';
config.toolbar = 'Full';
// 或自定義工具欄
config.toolbar_Full = [
{ name: 'basicstyles', items: ['Bold','Italic','Underline'] },
{ name: 'paragraph', items: ['NumberedList','BulletedList'] },
{ name: 'insert', items: ['Image','Table','HorizontalRule'] },
'/',
{ name: 'styles', items: ['Format','Font','FontSize'] },
{ name: 'colors', items: ['TextColor','BGColor'] }
];
require_once ROOT_PATH . 'ckeditor/ckeditor.php';
$CKEditor = new CKEditor();
$CKEditor->returnOutput = true;
$CKEditor->basePath = '/ckeditor/';
$contentArea = $CKEditor->editor('content', $rs['contents']);
echo $contentArea;
session_start();
function CheckAuthentication() {
return isset($_SESSION['UseEditor']) && $_SESSION['UseEditor'] === true;
}
if (isset($_SESSION['UseEditor'])) {
switch ($_SESSION['UseEditor']) {
case 'Addr1': $baseUrl = '/addr1/uploadfile/'; break;
case 'Addr2': $baseUrl = '/addr2/upfiles/'; break;
default: $baseUrl = '/upfiles/';
}
} else {
$baseUrl = '/upfiles/';
}
修改 core/connector/php/php5/CommandHandler/FileUpload.php:
$sUnsafeFileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding(
CKFinder_Connector_Utils_Misc::mbBasename($uploadedFile['name'])
);
// 新增:按時間戳重命名
$sExtension = CKFinder_Connector_Utils_FileSystem::getExtension($sUnsafeFileName);
$sUnsafeFileName = date('YmdHis') . '.' . $sExtension;
require_once ROOT_PATH . 'ckeditor/ckeditor.php';
require_once ROOT_PATH . 'ckfinder/ckfinder.php';
$CKEditor = new CKEditor();
$CKEditor->basePath = '/ckeditor/';
CKFinder::SetupCKEditor($CKEditor, '/ckfinder/'); // 相對站點根目錄
echo $CKEditor->editor('content', $rs['contents']);
CheckAuthentication 必須驗證用戶身份與權限,不可直接 return true。$config['AllowedExtensions'] 限制上傳後綴。755,禁止腳本執行(Nginx/Apache 配置 php_flag engine off)。整合後可實現圖片上傳、文件管理、圖片瀏覽器一站式體驗,適合 CMS、後台管理系統等場景。