杉哥的个人博客

thinkphp UEditor编辑器添加在线图片&&在线附件删除功能

通过ueditor来编辑文章,其中可以上传些图片附件,文章删除只能删除数据库中的图片路径,并不会把文件删除,久而久之就会有大量的垃圾文件。用以下方法给Ueditor编辑器添加附件删除功能。

第一,需要添加一个 php 文件来实现删除功能,文件添加到:ueditor\php\action_delete.php 代码内容:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
/*---------------------------
* Flyヽ
* 2016-02-04
* action_delete.php
* 删除 Ueditor 目录下的文件
*---------------------------*/
try {
//获取路径
$path = $_POST['path'];
$path = str_replace('../', '', $path);
$path = str_replace('/', '\\', $path);
//安全判断(只允许删除 ueditor 目录下的文件)
if(stripos($path, '\\ueditor\\') !== 0)
{
return '非法删除';
}
//获取完整路径
$path = $_SERVER['DOCUMENT_ROOT'].$path;
if(file_exists($path)) {
//删除文件
unlink($path);
return 'ok';
} else {
return '删除失败,未找到'.$path;
}
} catch (Exception $e) {
return '删除异常:'.$e->getMessage();
}
<?php /*--------------------------- * Flyヽ * 2016-02-04 * action_delete.php * 删除 Ueditor 目录下的文件 *---------------------------*/ try { //获取路径 $path = $_POST['path']; $path = str_replace('../', '', $path); $path = str_replace('/', '\\', $path); //安全判断(只允许删除 ueditor 目录下的文件) if(stripos($path, '\\ueditor\\') !== 0) { return '非法删除'; } //获取完整路径 $path = $_SERVER['DOCUMENT_ROOT'].$path; if(file_exists($path)) { //删除文件 unlink($path); return 'ok'; } else { return '删除失败,未找到'.$path; } } catch (Exception $e) { return '删除异常:'.$e->getMessage(); }
<?php
/*---------------------------
* Flyヽ
* 2016-02-04
* action_delete.php
* 删除 Ueditor 目录下的文件
*---------------------------*/
try {
    //获取路径
    $path = $_POST['path'];
    $path = str_replace('../', '', $path);
    $path = str_replace('/', '\\', $path);
    
    //安全判断(只允许删除 ueditor 目录下的文件)
    if(stripos($path, '\\ueditor\\') !== 0)
    {
        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 的处理:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
switch ($action) {
//....
/* 删除图片命令处理 */
case 'deleteimage':
$result = include('action_delete.php');
break;
/* 在 default 之前添加 */
default:
$result = json_encode(array(
'state'=> '请求地址出错'
));
break;
}
switch ($action) { //.... /* 删除图片命令处理 */ case 'deleteimage': $result = include('action_delete.php'); break; /* 在 default 之前添加 */ default: $result = json_encode(array( 'state'=> '请求地址出错' )); break; }
switch ($action) {
    //....
  
    /* 删除图片命令处理 */
    case 'deleteimage':
         $result = include('action_delete.php');
         break;
    
    /* 在 default 之前添加 */
    default:
        $result = json_encode(array(
            'state'=> '请求地址出错'
        ));
        break;
}

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* 在这两句之后添加 */
item.appendChild(img);
item.appendChild(icon);
/* 添加删除功能 */
item.appendChild($("<span class='delbtn' url='" + list[i].url + "'>✖</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]);
/* 在这一句之前添加 */
this.list.insertBefore(item, this.clearFloat);
/* 在这两句之后添加 */ item.appendChild(img); item.appendChild(icon); /* 添加删除功能 */ item.appendChild($("<span class='delbtn' url='" + list[i].url + "'>✖</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]); /* 在这一句之前添加 */ this.list.insertBefore(item, this.clearFloat);
/* 在这两句之后添加 */
item.appendChild(img);
item.appendChild(icon);
/* 添加删除功能 */
item.appendChild($("<span class='delbtn' url='" + list[i].url + "'>✖</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]);
/* 在这一句之前添加 */
this.list.insertBefore(item, this.clearFloat);

第四,为删除按钮添加一个样式,修改文件:ueditor\dialogs\image\image.css 在最底部添加如下代码:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* 在线管理删除按钮样式 */
#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;
}
/* 在线管理删除按钮样式 */ #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; }
/* 在线管理删除按钮样式 */
#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;
}

若需要增加鼠标滑动显示删除按钮及在线附件删除功能请参考以下:

打开ueditor\dialogs\image\image.js找到

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
this.list.insertBefore(item, this.clearFloat);
this.list.insertBefore(item, this.clearFloat);
this.list.insertBefore(item, this.clearFloat);

在这句语句的下面插入:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/* 添加删除按钮滑动事件 */
$(item).mouseenter(function () {
$(this).find('.delbtn').css('display','block');
}).mouseleave(function(){
$(this).find('.delbtn').css('display','none');
});
/* 添加删除按钮滑动事件 */ $(item).mouseenter(function () { $(this).find('.delbtn').css('display','block'); }).mouseleave(function(){ $(this).find('.delbtn').css('display','none'); });
/* 添加删除按钮滑动事件 */
$(item).mouseenter(function () {
    $(this).find('.delbtn').css('display','block');
}).mouseleave(function(){
    $(this).find('.delbtn').css('display','none');
});

添加附件删除功能:

打开\dialogs\attachment\attachment.js找到

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
item.appendChild(icon);
item.appendChild(icon);
item.appendChild(icon);

在此句语句下添加:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
item.appendChild($("<span class='delbtn' style='display:none;' url='" + list[i].url + "'>✖</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]);
/* 添加删除按钮滑动事件 */
$(item).mouseenter(function () {
$(this).find('.delbtn').css('display','block');
}).mouseleave(function(){
$(this).find('.delbtn').css('display','none');
});
打开ueditor\dialogs\attachment\attachment.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;
}
item.appendChild($("<span class='delbtn' style='display:none;' url='" + list[i].url + "'>✖</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]); /* 添加删除按钮滑动事件 */ $(item).mouseenter(function () { $(this).find('.delbtn').css('display','block'); }).mouseleave(function(){ $(this).find('.delbtn').css('display','none'); }); 打开ueditor\dialogs\attachment\attachment.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; }
item.appendChild($("<span class='delbtn' style='display:none;' url='" + list[i].url + "'>✖</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]);

/* 添加删除按钮滑动事件 */
$(item).mouseenter(function () {
    $(this).find('.delbtn').css('display','block');
}).mouseleave(function(){
    $(this).find('.delbtn').css('display','none');
});
打开ueditor\dialogs\attachment\attachment.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;
}

在线图片效果如下: