thinkphp5.1对接ueditor(自定义上传接口)

由于ueditor官方提供了 php的接口,所以很多人都是直接使用他自带的那个了,但是如果要用到thinkphp我们自己开发的上传接口,则官方没有给出更多的解决方案,

解决方法:

(1)自己封装一个上传类:ueditor.php

class Ueditor extends Base{
	
	// 优先加载
	protected function initialize() {
		parent::initialize();
		$this->attachmentModel = new AttachmentModel;
		$this->configModel = new ConfigModel;
		$this->config =$this->configModel->getConfig('upload');
		$this->config['max_size'] = $this->config['max_size'] * 1024;
		$this->config['allow_ext'] =  str_replace('|',',',$this->config['allow_ext']);
	}
	
	// 编辑器内上传图片
	private $stateMap = array(
		'SUCCESS', //上传成功标记,在UEditor中内不可改变,否则flash判断会出错
		'文件大小超出 upload_max_filesize 限制',
		'文件大小超出 MAX_FILE_SIZE 限制',
		'文件未被完整上传',
		'没有文件被上传',
		'上传文件为空',
		'ERROR_TMP_FILE' => '临时文件错误',
		'ERROR_TMP_FILE_NOT_FOUND' => '找不到临时文件',
		'ERROR_SIZE_EXCEED' => '文件大小超出网站限制',
		'ERROR_TYPE_NOT_ALLOWED' => '文件类型不允许',
		'ERROR_CREATE_DIR' => '目录创建失败',
		'ERROR_DIR_NOT_WRITEABLE' => '目录没有写权限',
		'ERROR_FILE_MOVE' => '文件保存时出错',
		'ERROR_FILE_NOT_FOUND' => '找不到上传文件',
		'ERROR_WRITE_CONTENT' => '写入文件内容错误',
		'ERROR_UNKNOWN' => '未知错误',
		'ERROR_DEAD_LINK' => '链接不可用',
		'ERROR_HTTP_LINK' => '链接不是http链接',
		'ERROR_HTTP_CONTENTTYPE' => '链接contentType不正确',
		'INVALID_URL' => '非法 URL',
		'INVALID_IP' => '非法 IP'
	);
	
	// Ueditor 方法 Controller.php,初始化
	public function doupload() {
		date_default_timezone_set('Asia/chongqing');
		error_reporting(E_ERROR);
		header('Content-Type: text/html; charset=utf-8');
		$CONFIG = json_decode(preg_replace('/\/\*[\s\S]+?\*\//', '', file_get_contents(PUBLIC_PATH.'/static/common/ueditor/ueditor.config.js')), true);
		$action = input('param.action');

		$uploadfile = ROOT_PATH . 'upload' . DS . 'image';

		switch ($action) {
			case 'config':
				$result =  json_encode($CONFIG);
				break;
			
			/* 上传图片 */
			case 'uploadimage':
				$result = $this->upload_image($uploadfile);
				break;
			
			/* 上传涂鸦 */
			case 'uploadscrawl':
			
			/* 上传视频 */
			case 'uploadvideo':
			
			/* 上传文件 */
			
			case 'uploadfile':
				//$result = include("action_upload.php");
				break;
			
			/* 列出图片 */
			case 'listimage':
				//$result = include("action_list.php");
				break;
			
			/* 列出文件 */
			case 'listfile':
				//$result = include("action_list.php");
				break;
			
			/* 抓取远程文件 */
			case 'catchimage':
				//$result = $this -> get_remote_image($mySavePath);
				break;
			
			default:
				$result = json_encode(array(
					'state'=> '请求地址出错'
				));
				break;
		}
		
		/* 输出结果 */
		if(isset($_GET['callback'])){
			if (preg_match('/^[\w_]+$/', $_GET['callback'])) {
				echo htmlspecialchars($_GET['callback']) . '(' . $result . ')';
			}else{
				echo json_encode(array(
					'state'=> 'callback参数不合法'
				));
			}
		} else {
			echo $result;
		}
	}
	
	// 上传图片
	public function upload_image($uploadfile) {
       //您的图片上传方法
    }
}

2、在需要用到富文本编辑器的地方添加js代码:

<script src="/public/static/common/ueditor/ueditor.config.js"></script>
<script src="/public/static/common/ueditor/ueditor.all.min.js"></script>
<script>
			UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
			UE.Editor.prototype.getActionUrl = function(action) {
			    if (action == "uploadimage") {    //上传图片
			        return "{url('admin/ueditor/doupload',['action'=>'uploadimage'])}";
			    } else  if(action == "config") {    //加载配置
			        return this._bkGetActionUrl.call(this, action);
			    }
			}
			var ue = UE.getEditor("editor_content",{
				autoHeightEnabled: false,  //禁止自动长高
				autoFloatEnabled:false,    //禁止工具条漂浮
			});
			</script

刷新即可,不需要去专门配置ueditor.config.js里边的url了

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值