关于ueditor1.4.3和thinkphp3.2整合的问题解决方案

在ueditor1.4.3中将上传部分改成了统一管理,并设定了一个统一管理的控制层。

我的方案就是这里开始,第一步初始化ueditor1.4.3

<script>
	window.UEDITOR_HOME_URL = "__PUBLIC__/static/ueditor143/";  
    window.οnlοad=function(){
        window.UEDITOR_CONFIG.initialFrameHeight=300;//编辑器的高度
        window.UEDITOR_CONFIG.serverUrl="{:U('File/ue_controller')}";          //管理的网址地址
        UE.getEditor('editor');//我的textarea的id值
       
        }
</script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/static/ueditor143/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/static/ueditor143/ueditor.all.min.js">
</script>

第二步转到我的控制层FileController

先看代码

<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Image;
/**
 * 文件控制器
 * 主要用于下载模型的文件上传和下载
 */
class FileController extends Controller {

	/**
	 * 上传图片
	 * @author huajie <banhuajie@163.com>
	 */
	public function uploadPicture() {
		//TODO: 用户登录检测

		/* 返回标准数据 */
		$return = array('status' => 1, 'info' => '上传成功', 'data' => '');

		/* 调用文件上传组件上传文件 */
		$Picture = D('Picture');
		$pic_driver = C('PICTURE_UPLOAD_DRIVER');
		$info = $Picture -> upload($_FILES, C('PICTURE_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
		//TODO:上传到远程服务器

		if ($info) {
			$return['status'] = 1;
			$return = array_merge($info['download'], $return);

		} else {
			$return['status'] = 0;
			$return['info'] = $Picture -> getError();
		}

		/* 返回JSON数据 */
		$this -> ajaxReturn($return);
	}
//从这里开始才是本文需要的代码
//
	public function ue_controller() {
		$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./Public/Admin/conf/config.json")), true);
		//读取ueditor上传配置
		$action = $_GET['action'];
		//$result = array();
		switch ($action) {
			case 'config' :
				$result = $CONFIG;
				break;
			/* 上传图片 */
			case 'uploadimage' :
			/* 上传涂鸦 */
			case 'uploadscrawl' :
			/* 上传视频 */
			case 'uploadvideo' :
			/* 上传文件 */
			case 'uploadfile' :
				$info =	$this->ue_upimg();
//				var_dump($info);
				if ($info) {
					$savename = $info['upfile']['path'];
					//返回文件地址和名给JS作回调用
					$result = array('url' => __ROOT__ .  $savename, 'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $info['upfile']['name'], 'state' => 'SUCCESS');
					//echo $info;
				} else {
					//$this->error($upload->getError());//获取失败信息
					$result = array('state' => $upload -> getError(), );
				}
				break;
		}
		/* 输出结果 */
		if (isset($_GET["callback"])) {
			if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
				$result = json_encode($result);
				echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
			} else {
				echo json_encode(array('state' => 'callback参数不合法'));
			}
		} else {
			$this -> ajaxReturn($result);
		}
	}

	public function ue_upimg() {
		$Picture = D('Picture');
		$pic_driver = C('PICTURE_UPLOAD_DRIVER');
		$info = $Picture -> upload($_FILES, C('PICTURE_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
		return $info;
	}
	
	public function lists() {
		//这个地方没有写,大家可以从数据库调取了
		//$this->ajaxReturn($result);
	}

}
第三步看model,直接用的是OneThink

<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Model;
use Think\Model;
use Think\Upload;

/**
 * 图片模型
 * 负责图片的上传
 */

class PictureModel extends Model {
	/**
	 * 自动完成
	 * @var array
	 */
	protected $_auto = array( array('status', 1, self::MODEL_INSERT), array('create_time', NOW_TIME, self::MODEL_INSERT), );

	/**
	 * 文件上传
	 * @param  array  $files   要上传的文件列表(通常是$_FILES数组)
	 * @param  array  $setting 文件上传配置
	 * @param  string $driver  上传驱动名称
	 * @param  array  $config  上传驱动配置
	 * @return array           文件上传成功后的信息
	 */
	public function upload($files, $setting, $driver = 'Local', $config = null) {
		/* 上传文件 */
		$setting['callback'] = array($this, 'isFile');
		$setting['removeTrash'] = array($this, 'removeTrash');
		$Upload = new Upload($setting, $driver, $config);
		$info = $Upload -> upload($files);

		if ($info) {//文件上传成功,记录文件信息
			foreach ($info as $key => &$value) {
				/* 已经存在文件记录 */
				if (isset($value['id']) && is_numeric($value['id'])) {
					continue;
				}

				/* 记录文件信息 */
				$value['path'] = substr($setting['rootPath'], 1) . $value['savepath'] . $value['savename'];
				//在模板里的url路径
				if ($this -> create($value) && ($id = $this -> add())) {
					$value['id'] = $id;
				} else {
					//TODO: 文件上传成功,但是记录文件信息失败,需记录日志
					unset($info[$key]);
				}
			}
			return $info;
			//文件上传成功
		} else {
			$this -> error = $Upload -> getError();
			return false;
		}
	}

	public function getImagePath($id) {
		$file = $this -> find($id);
		if (!$file) {
			$this -> error = '不存在该文件!';
			return false;
		} else {
			return $file['path'];
		}

	}

	/**
	 * 下载指定文件
	 * @param  number  $root 文件存储根目录
	 * @param  integer $id   文件ID
	 * @param  string   $args     回调函数参数
	 * @return boolean       false-下载失败,否则输出下载文件
	 */
	public function download($root, $id, $callback = null, $args = null) {
		/* 获取下载文件信息 */
		$file = $this -> find($id);
		if (!$file) {
			$this -> error = '不存在该文件!';
			return false;
		}

		/* 下载文件 */
		switch ($file['location']) {
			case 0 :
				//下载本地文件
				$file['rootpath'] = $root;
				return $this -> downLocalFile($file, $callback, $args);
			case 1 :
				//TODO: 下载远程FTP文件
				break;
			default :
				$this -> error = '不支持的文件存储类型!';
				return false;
		}

	}

	/**
	 * 检测当前上传的文件是否已经存在
	 * @param  array   $file 文件上传数组
	 * @return boolean       文件信息, false - 不存在该文件
	 */
	public function isFile($file) {
		if (empty($file['md5'])) {
			throw new \Exception('缺少参数:md5');
		}
		/* 查找文件 */
		$map = array('md5' => $file['md5'], 'sha1' => $file['sha1'], );
		return $this -> field(true) -> where($map) -> find();
	}

	/**
	 * 下载本地文件
	 * @param  array    $file     文件信息数组
	 * @param  callable $callback 下载回调函数,一般用于增加下载次数
	 * @param  string   $args     回调函数参数
	 * @return boolean            下载失败返回false
	 */
	private function downLocalFile($file, $callback = null, $args = null) {
		if (is_file($file['rootpath'] . $file['savepath'] . $file['savename'])) {
			/* 调用回调函数新增下载数 */
			is_callable($callback) && call_user_func($callback, $args);

			/* 执行下载 */ //TODO: 大文件断点续传
			header("Content-Description: File Transfer");
			header('Content-type: ' . $file['type']);
			header('Content-Length:' . $file['size']);
			if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) {//for IE
				header('Content-Disposition: attachment; filename="' . rawurlencode($file['name']) . '"');
			} else {
				header('Content-Disposition: attachment; filename="' . $file['name'] . '"');
			}
			readfile($file['rootpath'] . $file['savepath'] . $file['savename']);
			exit ;
		} else {
			$this -> error = '文件已被删除!';
			return false;
		}
	}

	/**
	 * 清除数据库存在但本地不存在的数据
	 * @param $data
	 */
	public function removeTrash($data) {
		$this -> where(array('id' => $data['id'], )) -> delete();
	}

}

现在已经完成全部代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值