UEditor 编辑器,图片【在线管理】添加删除功能

本文介绍如何在UEditor中实现图片的删除功能,包括创建删除PHP文件、修改控制器、添加删除按钮及样式。通过安全判断确保仅能删除指定目录下的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法来源于网络​​​​​​​

1、新建一个删除的php文件,路径: ueditor\dialogs\image\action_delete.php

<?php
/*---------------------------
 * action_delete.php
 * 删除 ueditor 目录下的文件
 *---------------------------*/
function safe_replace($string) {
    $string = trim($string);
    $string = str_replace(array('\\',';','\'','%2527','%27','%20','&', '"', '<', '>'), array('','','','','','','&amp;', '&quot;', '&lt;', '&gt;'), $string);
    $string=nl2br($string); 
    return $string;
}
try{

    $path = safe_replace($_POST['path']);
    $path = str_replace('../', '', $path);
    $path = str_replace('/', '\\', $path);

    //安全判断(只允许删除 ueditor 目录下的文件)
    if(stripos($path, '\\ueditor\\') == 0 || stripos($path, '\\ueditor\\') === false)
    {
        return '非法删除';
    }

    //获取完整路径
    $path = $_SERVER['DOCUMENT_ROOT'].$path;
    if(file_exists($path)) {
        //删除文件
        unlink($path);
        return 'ok';
    } else {
        return '删除失败,未找到'.$path;
    }

}catch (Exception $e) {
    return '删除异常:'.$e->getMessage();
}
?>

2、添加删除文件的方法,路径:ueditor\php\controller.php

/* 删除图片命令处理 */
case 'deleteimage':
    $result = include('action_delete.php');
    break;

3、添加删除按钮,js路径:ueditor\dialogs\image\image.js

item.appendChild(img);
item.appendChild(icon);

/* 添加删除功能 Start*/
item.appendChild($("<span class='delbtn' url='" + list[i].url + "'>X</span>").click(function() {
    var del = $(this);
    try{
        window.event.cancelBubble = true; //停止冒泡
        window.event.returnValue = false; //阻止事件的默认行为
        window.event.preventDefault();    //取消事件的默认行为  
        window.event.stopPropagation();   //阻止事件的传播
    }finally{
        if(!confirm("确定要删除吗?")) return;
        $.post(editor.getOpt("serverUrl") + "?action=deleteimage", { "path": del.attr("url") }, function(result) {
            if (result == "ok") del.parent().remove();
            else alert(result);
            });
        }
    })[0]);
/* 添加删除功能 End*/

this.list.insertBefore(item, this.clearFloat);

4、添加删除按钮样式,路径:ueditor\dialogs\image\image.css

#online li .delbtn {      
    position: absolute;
    top: 0;
    right: 0;
    border: 0;   
    z-index: 3;
    color: #ffffff;
    display: inline;
    font-size: 12px;
    line-height: 10.5px;
    padding:3px 5px;
    text-align: center;
    background-color: #d9534f;
}

最终效果:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值