UEditor1.4.3添加在线管理图片删除功能方法

发布期:2018-12-06 | 来源处:本站 | 编辑者:admin | 浏览量:0
06
12
2018

第一,需要添加一个 php 文件来实现删除功能,文件添加到: ueditor /php /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();
}
?>


第二,需要在 ueditor /php /controller.php 文件的 switch 中添加命令 deleteimage 的处理:

switch ($action) {
 
    /* 删除图片命令处理 */
    case 'deleteimage':
         $result = include("action_delete.php");
         break;
    
    /* 在 default 之前添加 */
    default:
        $result = json_encode(array(
            'state'=> '请求地址出错'
        ));
        break;
 }

   


第三,在图片上添加删除按钮,需要修改 Js 文件:ueditor /dialogs /image /image.js

 /* 在这两句之后添加 */
item.appendChild(img);item.appendChild(icon);
 /* 添加删除功能 */
item.appendChild($("").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]);
 /* 在这一句之前添加 */
this.list.insertBefore(item, this.clearFloat);


第四,为删除按钮添加一个样式,修改文件: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;
  width: 4px;
  height: 10px;
  background-image: url(images/gtk-del.png);
  background-repeat: no-repeat;
}

下面这段是设背景图,可自己添加删除图,不然无法显示删除按钮,只显示按钮颜色,虽然可以用,但不美观。

 background-image: url(images/gtk-del.png);





TAG 标签:

网站优化专区

本月热点资讯
热线(微信同号):
137-6042-7131
在线客服:
售前
售前
售前
售后
微信图标
联系微信:
微信联系二维码
Hi,Are you ready?
准备好开始了吗?
那就与我们取得联系吧

咨询送礼现在提交,将获得全宝鑫公司策划专家免费为您设计
创意LOGO,免费设计您满意的名片。
下单送礼感恩多年,新老用户下单即送创业型空间+域名等大礼
8:30-18:00小时免费咨询热线137-6042-7131
建站留言合作意向表

您需要的服务

您关注的地方

您大概的预算

直接咨询