说ueditor上传图片功能
借鉴了ueditor自带的php代码,和ot中的相关代码。
此代码已发布到我的csdn当中<?php <br />
// +----------------------------------------------------------------------<br>
// | UploaderController <br>
// +----------------------------------------------------------------------<br>
// | Copyright (c) 2015 AntDz All rights reserved.<br>
// +----------------------------------------------------------------------<br>
// | Author: antdz <vip_antdz><br>
// +----------------------------------------------------------------------<br>
namespace Admin\Controller;<br>
use Think\Controller;<br>
@ini_set('upload_max_filesize', '20M');<br>
<br>
class UploaderController extends Controller {<br>
public function index() {<br>
$this -> display();<br>
}<br>
<br>
/**<br>
* 用于百度ueditor<br>
* 其中UE_CONFIG在config文件中定义<br>
*/<br>
<br>
public function manager() {<br>
date_default_timezone_set("Asia/Chongqing");<br>
<br>
$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(C('UE_CONFIG'))), true);<br>
$action = $_GET['action'];<br>
<br>
switch ($action) {<br>
case 'config' :<br>
$result = $CONFIG;<br>
break;<br>
case 'listfile' :<br>
case 'listimage' :<br>
$result = $this -> lists($CONFIG, $action);<br>
break;<br>
/* 上传图片 */<br>
case 'uploadimage' :<br>
/* 上传涂鸦 */<br>
case 'uploadscrawl' :<br>
$config = array('maxSize' => $CONIFG['imageMaxSize'], 'rootPath' => C('UPLOAD_PATH'), 'savePath' => 'images/', 'saveName' => array('uniqid', ''), 'exts' => $CONIFG['imageAllowFiles'], 'autoSub' => true, 'subName' => array('date', 'Ymd'), );<br>
$result = $this -> up($config);<br>
break;<br>
/* 上传视频 */<br>
case 'uploadvideo' :<br>
$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'), );<br>
$result = $this -> up($config);<br>
break;<br>
/* 上传文件 */<br>
case 'uploadfile' :<br>
default :<br>
$config = array('maxSize' => $CONIFG['fileMaxSize'], 'rootPath' => C('UPLOAD_PATH'), 'savePath' => 'files/', 'saveName' => array('uniqid', ''), 'exts' => $CONIFG['fileManagerAllowFilesarray'], 'autoSub' => true, 'subName' => array('date', 'Ymd'), );<br>
$result = $this -> up($config);<br>
break;<br>
}<br>
/* 输出结果 */<br>
if (isset($_GET["callback"])) {<br>
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {<br>
$this -> ajaxReturn($result, 'JSON');<br>
} else {<br>
$tmp = array('state' => 'callback参数不合法');<br>
$this -> ajaxReturn($tmp, 'JSON');<br>
}<br>
} else {<br>
//echo $result;<br>
$this -> ajaxReturn($result, 'JSON');<br>
}<br>
}<br>
<br>
/**<br>
* 用于百度编辑器上传功能<br>
*/<br>
<br>
public function up($config) {<br>
//TODO:删除不删除?<br>
@ini_set('upload_max_filesize', '20M');<br>
<br>
$Picture = D('Picture');<br>
$file_driver = C('PICTURE_UPLOAD_DRIVER');<br>
$info = $Picture -> upload($_FILES, $config, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$file_driver}_CONFIG"));<br>
<br>
if ($info) {<br>
if ($info['upfile']['url'] == "") {<br>
return array('url' => __ROOT__ . $info['upfile']['path'], 'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $info['upfile']['name'], 'state' => 'SUCCESS');<br>
} else {<br>
return array('url' => $info['upfile']['url'], 'title' => htmlspecialchars($_POST['pictitle'], ENT_QUOTES), 'original' => $info['upfile']['name'], 'state' => 'SUCCESS');<br>
}<br>
<br>
} else {<br>
return array('state' => $Picture -> getError());<br>
//获取失败信息<br>
}<br>
}<br>
<br>
/**<br>
*百度编辑器列出本地服务器上传目录<br>
*<br>
*<br>
**/<br>
public function lists($config, $type) {<br>
switch ($type) {<br>
case 'listfile' :<br>
$allowFiles = $config['fileManagerAllowFiles'];<br>
$listSize = $config['fileManagerListSize'];<br>
$path = $config['fileManagerListPath'];<br>
break;<br>
<br>
default :<br>
$allowFiles = $config['imageManagerAllowFiles'];<br>
$listSize = $config['imageManagerListSize'];<br>
$path = $config['imageManagerListPath'];<br>
break;<br>
}<br>
<br>
$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);<br>
/* 获取参数 */<br>
$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;<br>
$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;<br>
$end = $start + $size;<br>
<br>
/* 获取文件列表 */<br>
$path = DOC_ROOT . C('UPLOAD_PATH');<br>
$files = $this -> getfiles($path, $allowFiles);<br>
if (!count($files)) {<br>
return array("state" => "no match file", "list" => array(), "start" => $start, "total" => count($files));<br>
}<br>
<br>
/* 获取指定范围的列表 */<br>
$len = count($files);<br>
for ($i = min($end, $len) - 1, $list = array(); $i = 0 && $i >= $start; $i--) {<br>
$list[] = $files[$i];<br>
}<br>
<br>
$result = array("state" => "SUCCESS", "list" => $list, "start" => $start, "total" => count($files));<br>
return $result;<br>
}<br>
<br>
/**<br>
* 获取本地文件<br>
*/<br>
<br>
function getfiles($path, $allowFiles, &$files = array()) {<br>
if (!is_dir($path)) {<br>
exit('E1:path is wrong ' . $path);<br>
return null;<br>
}<br>
if (substr($path, strlen($path) - 1) != '/')<br>
$path .= '/';<br>
$handle = opendir($path);<br>
while (false !== ($file = readdir($handle))) {<br>
if ($file != '.' && $file != '..') {<br>
$path2 = $path . $file;<br>
if (is_dir($path2)) {<br>
$this -> getfiles($path2, $allowFiles, $files);<br>
} else {<br>
if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {<br>
$files[] = array('url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])), 'mtime' => filemtime($path2));<br>
}<br>
}<br>
}<br>
}<br>
return $files;<br>
}<br>
<br>
/**<br>
* 上传图片<br>
* @author huajie <banhuajie><br>
*/<br>
public function uploadPicture() {<br>
<br>
/* 返回标准数据 */<br>
$return = array('status' => 1, 'info' => '上传成功', 'data' => '');<br>
<br>
/* 调用文件上传组件上传文件 */<br>
$Picture = D('Picture');<br>
$file_driver = C('PICTURE_UPLOAD_DRIVER');<br>
$info = $Picture -> upload($_FILES, C('PICTURE_UPLOAD'), C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$file_driver}_CONFIG"));<br>
//TODO:上传到远程服务器<br>
<br>
/* 记录图片信息 */<br>
if ($info) {<br>
$return['status'] = 1;<br>
$return = array_merge($info['download'], $return);<br>
} else {<br>
$return['status'] = 0;<br>
$return['info'] = $Picture -> getError();<br>
}<br>
<br>
/* 返回JSON数据 */<br>
$this -> ajaxReturn($return);<br>
}<br>
<br>
}</banhuajie></vip_antdz>
AD:真正免费,域名+虚机+企业邮箱=0元