ThinkPHP&&YII1 UEditor 添加在线图片&&在线附件删除功能
TP框架请参考以下:
第一,需要添加一个 php 文件来实现删除功能,文件添加到:ueditor\php\action_delete.php 代码内容:
<?php
/*---------------------------
* Flyヽ
* http://lfei.org
* 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 的处理:
....
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($("<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 在最底部添加如下代码:
/* 在线管理删除按钮样式 */
#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;
}以上教程转载:http://lfei.org/ueditor-manage-image-delete/
若需要增加鼠标滑动显示删除按钮及在线附件删除功能请参考以下:
打开ueditor\dialogs\image\image.js找到
this.list.insertBefore(item, this.clearFloat);
在这句语句的下面插入:
/* 添加删除按钮滑动事件 */
$(item).mouseenter(function () {
$(this).find('.delbtn').css('display','block');
}).mouseleave(function(){
$(this).find('.delbtn').css('display','none');
});添加附件删除功能:
打开ueditor\dialogs\attachment\attachment.js找到
item.appendChild(icon);
在次句语句下添加:
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;
}'application.extensions.baiduUeditor.*',在线图片效果如下:

在线附件效果如下:
YII Ueditor包下载地址:https://github.com/q250305917/Ueditor-for-YII1
下载后解压放在\protected\extensions\下
打开main.php配置文件,在import里面增加:
将\protected\extensions\baiduUeditor\UeditorController.php拷贝至模块控制器中,在视图页面绑定使用参考:
<?php
//KindEditor
$this->widget('ext.baiduUeditor.UeditorWidget',
array(
'id'=>'NoticeFormV3_text',//容器的id 唯一的[必须配置]
'name'=>'content',//post到后台接收的name [必须配置]
'content'=>'',//初始化内容 [可选的]
//配置选项,[可选的]
//将ueditor的配置项以数组键值对的方式传入,具体查看ueditor.config.js
//不要配置serverUrl(即使配置也会被覆盖)程序会自动处理后端url
'config'=>array(
'wordCount'=>false,
'elementPathEnabled'=>false,
"imageAllowFiles"=> array(".png", ".jpg", ".jpeg", ".gif", ".bmp"),
'toolbars'=>array(array('fontfamily', 'fontsize', 'forecolor', 'backcolor','|',
'indent', '|',
'fullscreen','|',
'bold', 'italic', 'underline','autotypeset', '|',
'customstyle',
'paragraph', '|',
'link', 'unlink', '|', 'imagenone', 'imageleft', 'imageright',
'imagecenter', '|','simpleupload','insertimage', 'emotion', 'insertvideo', 'attachment',)),//toolbars注意是嵌套两个数组
'lang'=>'zh-cn'
)
)
); ?>错误排除
出现错误首先应该打开调试工具查看请求返回具体信息。
因为编辑器通常使用场景为后台,所以图片上传等功能默认需要登录权限,如果不需要可以自行修改。
默认上传路径为「应用根目录」,而不是网站根目录,如果上传失败请查看目录权限。
不要开启Yii的调试,因为UEditor的返回都是json格式,开启调试会导致返回格式不识别。
出现404错误可能是因为widget没有正确配置serverUrl。
上传错误提示:后端配置项没有正常加载,上传插件不能正常使用!检查Yii是否开启调试,关闭调试,检查初始化UeditorController中用户是否具有访问权限
/* if(Yii::app()->user->isGuest){
echo json_encode(array('state'=>'没有权限'));
Yii::app()->end();
}*/
注释所有权限设置,重新打开上传页面,判断是否权限阻止上传