thinkphp整合ueditor上传功能


   
   
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
@ini_set('upload_max_filesize', '20M');

class UploaderController extends Controller {
	public function index() {
		$this -> display();
	}

	/**
	 * 用于百度ueditor
	 * 其中UE_CONFIG在config文件中定义
	 */

	public function manager() {
		date_default_timezone_set("Asia/Chongqing");

		$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(C('UE_CONFIG'))), true);
		$action = $_GET['action'];

		switch ($action) {
			case 'config' :
				$result = $CONFIG;
				break;
			case 'listfile' :
			case 'listimage' :
				$result = $this -> lists($CONFIG, $action);
				break;
			/* 上传图片 */
			case 'uploadimage' :
			/* 上传涂鸦 */
			case 'uploadscrawl' :
				$config = array('maxSize' => $CONIFG['imageMaxSize'], 'rootPath' => C('UPLOAD_PATH'), 'savePath' => 'images/', 'saveName' => array('uniqid', ''), 'exts' => $CONIFG['imageAllowFiles'], 'autoSub' => true, 'subName' => array('date', 'Ymd'), );
				$result = $this -> up($config);
				break;
			/* 上传视频 */
			case 'uploadvideo' :
				$config = array('maxSize' => $CONIFG['videoMaxSize'], 'rootPath' => C('UPLOAD_PATH'), 'savePath' => 'videos/', 'saveName' => array('uniqid', ''), 'exts' => array('jpg', 'gif', 'png', 'jpeg', 'zip', 'doc', 'pdf'), 'autoSub' => true, 'subName' => array('date', 'Ymd'), );
				$result = $this -> up($config);
				break;
			//$config[exts] = $CONFIG['videoAllowFiles'];
			/* 上传文件 */
			case 'uploadfile' :
			default :
				$config = array('maxSize' => $CONIFG['fileMaxSize'], 'rootPath' => C('UPLOAD_PATH'), 'savePath' => 'files/', 'saveName' => array('uniqid', ''), 'exts' => $CONIFG['fileManagerAllowFilesarray'], 'autoSub' => true, 'subName' => array('date', 'Ymd'), );
				$result = $this -> up($config);
				break;
		}
		/* 输出结果 */
		if (isset($_GET["callback"])) {
			if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
				//echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
				$this -> ajaxReturn($result, 'JSON');
			} else {
				//echo json_encode(array('state' => 'callback参数不合法'));
				$tmp = array('state' => 'callback参数不合法');
				$this -> ajaxReturn($tmp, 'JSON');
			}
		} else {
			//echo $result;
			$this -> ajaxReturn($result, 'JSON');
		}
	}

	/**
	 * 用于百度编辑器上传功能
	 */

	public function up($config) {
		//TODO:删除不删除?
		@ini_set('upload_max_filesize', '20M');

		$Picture = D('Picture');
		$file_driver = C('PICTURE_UPLOAD_DRIVER');
		$info = $Picture -> upload($_FILES, $config, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$file_driver}_CONFIG"));

		if ($info) {
			//返回文件地址和名给JS作回调用
			if ($info['upfile']['url'] == "") {
				return array('url' => __ROOT__ . $info['upfile']['path'], 'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $info['upfile']['name'], 'state' => 'SUCCESS');
			} else {
				return array('url' => $info['upfile']['url'], 'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $info['upfile']['name'], 'state' => 'SUCCESS');
			}

		} else {
			return array('state' => $Picture -> getError());
			//获取失败信息
		}
	}

	/**
	 *百度编辑器列出本地服务器上传目录
	 *
	 *
	 **/
	public function lists($config, $type) {
		switch ($type) {
			case 'listfile' :
				$allowFiles = $config['fileManagerAllowFiles'];
				$listSize = $config['fileManagerListSize'];
				$path = $config['fileManagerListPath'];
				break;

			default :
				$allowFiles = $config['imageManagerAllowFiles'];
				$listSize = $config['imageManagerListSize'];
				$path = $config['imageManagerListPath'];
				break;
		}

		$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
		/* 获取参数 */
		$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
		$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
		$end = $start + $size;

		/* 获取文件列表 */
		$path = DOC_ROOT . C('UPLOAD_PATH');
		$files = $this -> getfiles($path, $allowFiles);
		if (!count($files)) {
			return array("state" => "no match file", "list" => array(), "start" => $start, "total" => count($files));
		}

		/* 获取指定范围的列表 */
		$len = count($files);
		for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
			$list[] = $files[$i];
		}

		$result = array("state" => "SUCCESS", "list" => $list, "start" => $start, "total" => count($files));
		return $result;
	}

	/**
	 * 获取本地文件
	 */

	function getfiles($path, $allowFiles, &$files = array()) {
		if (!is_dir($path)) {
			exit('E1:path is wrong ' . $path);
			return null;
		}
		if (substr($path, strlen($path) - 1) != '/')
			$path .= '/';
		$handle = opendir($path);
		while (false !== ($file = readdir($handle))) {
			if ($file != '.' && $file != '..') {
				$path2 = $path . $file;
				if (is_dir($path2)) {
					$this -> getfiles($path2, $allowFiles, $files);
				} else {
					if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
						$files[] = array('url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])), 'mtime' => filemtime($path2));
					}
				}
			}
		}
		return $files;
	}

	/**
	 * 上传图片
	 * @author huajie 
   
   
    
    
	 */
	public function uploadPicture() {

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

		/* 调用文件上传组件上传文件 */
		$Picture = D('Picture');
		$file_driver = C('PICTURE_UPLOAD_DRIVER');
		$info = $Picture -> upload($_FILES, C('PICTURE_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$file_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);
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值