UEditor (UE) 是百度開源的富文本編輯器,1.4.3 版本對 PHP 支持較好。本文涵蓋下載、部署、前後端配置、自定義上傳路徑、常見坑點全流程。
/public/ueditor/<!-- 頁面引入 -->
<script src="/ueditor/ueditor.config.js"></script>
<script src="/ueditor/ueditor.all.min.js"></script>
<script src="/ueditor/lang/zh-cn/zh-cn.js"></script>
<!-- 編輯器容器 -->
<script id="editor" type="text/plain" style="width:100%;height:400px;"></script>
<!-- 實例化 -->
<script>
var ue = UE.getEditor('editor', {
// 動態配置覆蓋默認值
toolbars: [
['fullscreen', 'source', '|', 'undo', 'redo', '|',
'bold', 'italic', 'underline', '|',
'insertimage', 'insertvideo', 'attachment']
],
autoHeightEnabled: true,
autoFloatEnabled: true,
// 關鍵:指定後端控制器地址
serverUrl: '/ueditor/php/controller.php'
});
</script>
UE 所有操作 (上傳、列圖、抓遠程) 均通過 GET controller.php?action=xxx 請求,配置統一在 php/config.json:
{
"imageActionName": "uploadimage",
"imageFieldName": "upfile",
"imageMaxSize": 2048000,
"imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
"imageCompressEnable": true,
"imageCompressBorder": 1600,
"imageInsertAlign": "none",
"imageUrlPrefix": "", // 圖片訪問路徑前綴
"imagePathFormat": "/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
"scrawlActionName": "uploadscrawl",
"scrawlFieldName": "upfile",
"scrawlMaxSize": 2048000,
"scrawlAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
"scrawlPathFormat": "/ueditor/php/upload/scrawl/{yyyy}{mm}{dd}/{time}{rand:6}",
"catcherActionName": "catchimage",
"catcherFieldName": "source",
"catcherMaxSize": 2048000,
"catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
"catcherPathFormat": "/ueditor/php/upload/catcher/{yyyy}{mm}{dd}/{time}{rand:6}",
"videoActionName": "uploadvideo",
"videoFieldName": "upfile",
"videoMaxSize": 102400000,
"videoAllowFiles": [".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"],
"videoPathFormat": "/ueditor/php/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}",
"fileActionName": "uploadfile",
"fileFieldName": "upfile",
"fileMaxSize": 51200000,
"fileAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"],
"filePathFormat": "/ueditor/php/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}",
"imageManagerActionName": "listimage",
"imageManagerListPath": "/ueditor/php/upload/image/",
"imageManagerListSize": 20,
"imageManagerUrlPrefix": "",
"imageManagerInsertAlign": "none",
"imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
"fileManagerActionName": "listfile",
"fileManagerListPath": "/ueditor/php/upload/file/",
"fileManagerListSize": 20,
"fileManagerUrlPrefix": "",
"fileManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"]
}
| 配置項 | 含義 | 建議設置 |
|---|---|---|
imagePathFormat | 上傳圖片保存路徑模板 | 按日期分目錄:/upload/image/{yyyy}{mm}{dd}/{time}{rand:6} |
imageUrlPrefix | 編輯器回顯圖片時的 URL 前綴 | 若圖片服務器分離,填 CDN 域名 |
imageMaxSize | 單位字節,2MB=2048000 | 根據業務調整 |
imageAllowFiles | 允許上傳的副檔名 | 嚴格限制,防止腳本上傳 |
排查:訪問 https://yourdomain/ueditor/php/controller.php?action=config 應返回合法 JSON。若報 404/500:
fileinfo, gd, mbstringcontroller.php 中 CONFIG_JSON_PATH 是否正確指向 config.jsonupload/ 目錄需 Web 服務器寫入權限 (Linux: chown www:www -R upload/)原因:返回的 state: "SUCCESS" 但 url 路徑不可訪問。
解決:設置正確的 imageUrlPrefix 或確保 imagePathFormat 生成的路徑可通過 Web 直接訪問。
檢查 php.ini:
upload_max_filesize = 50M
post_max_size = 50M
max_execution_time = 300
max_input_time = 300
UE 1.4.3 存在歷史 XSS 漏洞,建議:
// routes/web.php
Route::any('/ueditor/controller', 'UEditorController@handle');
// app/Http/Controllers/UEditorController.php
public function handle(Request $request) {
$action = $request->get('action');
switch ($action) {
case 'config':
return response()->json(json_decode(file_get_contents(public_path('ueditor/php/config.json')), true));
case 'uploadimage':
case 'uploadscrawl':
case 'uploadvideo':
case 'uploadfile':
return $this->upload($request, $action);
case 'listimage':
case 'listfile':
return $this->listFiles($request, $action);
// ... 其他 action
}
}
private function upload(Request $request, string $action) {
$file = $request->file('upfile');
// 驗證、存儲、返回標準 JSON 格式
return response()->json([
'state' => 'SUCCESS',
'url' => $savedUrl,
'title' => $file->getClientOriginalName(),
'original' => $file->getClientOriginalName()
]);
}
UEditor 1.4.3 PHP 版集成核心在於:前端指定 serverUrl → 後端 controller.php 讀取 config.json → 根據 action 分發處理 → 返回統一 JSON 格式。掌握此流程,可靈活遷移至任意 PHP 框架、自定義存儲 (S3/OSS)、添加水印/壓縮/審核等業務邏輯。注意安全維護,建議新項目優先選擇活躍維護的編輯器。