在 FCKeditor 圖片瀏覽/上傳對話框中,為每張圖片加上「刪除」連結,點擊即時透過 AJAX 呼叫後端刪除實體檔案,並自動重新整理列表,無需重整頁面。
路徑:fckeditor/editor/filemanager/browser/default/frmresourceslist.html
找到 oListManager.GetFileRowHtml 函式,替換為:
oListManager.GetFileRowHtml = function(fileName, fileUrl, fileSize) {
// 建立選擇連結
var sLink = '<a href="#" onclick="OpenFile(\'' + ProtectPath(fileUrl) + '\');return false;">選擇</a>';
// 取得檔案圖示
var sIcon = oIcons.GetIcon(fileName);
// 回傳含縮圖、選擇、刪除的 HTML 結構
return '<div class="piclist"><img alt="" src="' + fileUrl + '" height="100" border="0"><p>' +
sLink + ' <a href="#" onclick="deleteFile(\'' + ProtectPath(fileUrl) + '\');" style="color:#FF9933;"> 刪除 </a></p></div>';
};
並在 <head> 區塊引入 AJAX 腳本:
<script type="text/javascript" src="/js/ajaxdel.js"></script>
//******************************** 圖片刪除功能 ******************************************
var req; // XMLHttpRequest 物件
var rn = Math.ceil(Math.random() * 1000000);
var rnch = rn;
// 產生不重複隨機數防快取
function rndnum() {
while (rn == rnch) rn = Math.ceil(Math.random() * 1000000);
rnch = rn;
return rn;
}
// 刪除檔案主函式
function deleteFile(file) {
var url = "/ajaxpic.php?filePath=" + escape(file) + "&UD=" + rndnum();
if (window.XMLHttpRequest) { // 現代瀏覽器 + IE7+
req = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE6 及以下
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req) {
req.open("GET", url, true); // 非同步 GET
req.onreadystatechange = callback;
req.send(null);
}
}
// 回呼處理
function callback() {
if (req.readyState == 4) {
if (req.responseText == "1") {
alert('刪除成功!');
Refresh(); // FCKeditor 內建函式,重新載入檔案列表
} else {
alert('刪除失敗!');
}
}
}
<?php
$root = dirname(__FILE__); // 當前腳本所在目錄
$filePath = isset($_GET['filePath']) ? $_GET['filePath'] : '';
/* 安全性檢查(生產環境必須) */
// 1. 驗證使用者登入/權限
// 2. 正規化路徑:realpath($root . $filePath)
// 3. 確保路徑在允許上傳目錄內 (防止目錄遍歷 ../../etc/passwd)
// 4. 檢查副檔名白名單
if (unlink($root . $filePath)) {
echo '1'; // 成功
} else {
echo '0'; // 失敗
}
?>
$realPath = realpath($root . $filePath); if (strpos($realPath, realpath($root . '/UserFiles/')) !== 0) die('非法路徑');FCKeditor 已停止維護。CKEditor 4/5 內建 檔案管理器 (CKFinder, 或開源替代如 elFinder, FilePond) 完整支援上傳/刪除/重命名/資料夾管理/圖片編輯,且具備完善安全機制。建議直接遷移至 CKEditor 5 + 現代檔案管理方案,避免維護舊版自訂代碼。