最近弄一个图片 上传,可是用ueditor 自带的上传,如果不配置的话,上传的目录不在自己的项目中。
在网上找了好多,可是都是底版本的,新版本的还真是找到了一个,ueditor-thinkphp 这个打开可以去下载,
把里面的几个类弄出来,
PublicClass/FileStorage.class.php
<?php
/**
* 文件管理类
* @author Nintendov
*/
namespace PublicClass;
class FileStorage{
/**
* 操作句柄
* @var string
* @access protected
*/
static protected $handler ;
/**
* 连接分布式文件系统
* @access public
* @param string $type 文件类型
* @param array $options 配置数组
* @return void
*/
static public function connect($type='File',$options=array()) {
$class = 'PublicClass\\FileStorage\\Driver\\'.ucwords($type);
self::$handler = new $class($options);
}
static public function __callstatic($method,$args){
//调用缓存驱动的方法
if(method_exists(self::$handler, $method)){
return call_user_func_array(array(self::$handler,$method), $args);
}
}
}
PublicClass/Ueditor.class.php
<?php
namespace PublicClass;
/**
* Ueditor插件
* @author Nintendov
*/
class Ueditor {
//public $uid;//要操作的用户id 如有登录需要则去掉注释
private $output; //要输出的数据
private $st;
private $rootpath = '/Uploads';
public function __construct($uid = '') {
//uid 为空则导入当前会话uid
//if(''===$uid) $this->uid = session('uid');
FileStorage::connect(STORAGE_TYPE);
//导入设置
$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(C("UEDITOR_CONFIG_PATH"))), true);
$CONFIG["imageDelUrl"] =C("UEDITOR_CONFIG_IMG_DEL_URL");
$action = htmlspecialchars($_GET['action']);
switch ($action) {
case 'config':
$result = json_encode($CONFIG);
break;
case 'uploadimage':
$config = array(
"pathFormat" => $CONFIG['imagePathFormat'],
"maxSize" => $CONFIG['imageMaxSize'],
"allowFiles" => $CONFIG['imageAllowFiles']
);
$fieldName = $CONFIG['imageFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'uploadscrawl':
$config = array(
"pathFormat" => $CONFIG['scrawlPathFormat'],
"maxSize" => $CONFIG['scrawlMaxSize'],
"allowFiles" => $CONFIG['scrawlAllowFiles'],
"oriName" => "scrawl.png"
);
$fieldName = $CONFIG['scrawlFieldName'];
$result = $this->uploadBase64($config, $fieldName);
break;
case 'uploadvideo':
$config = array(
"pathFormat" => $CONFIG['videoPathFormat'],
"maxSize" => $CONFIG['videoMaxSize'],
"allowFiles" => $CONFIG['videoAllowFiles']
);
$fieldName = $CONFIG['videoFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'uploadfile':
// default:
$config = array(
"pathFormat" => $CONFIG['filePathFormat'],
"maxSize" => $CONFIG['fileMaxSize'],
"allowFiles" => $CONFIG['fileAllowFiles']
);
$fieldName = $CONFIG['fileFieldName'];
$result = $this->uploadFile($config, $fieldName);
break;
case 'listfile':
$config = array(
'allowFiles' => $CONFIG['fileManagerAllowFiles'],
'listSize' => $CONFIG['fileManagerListSize'],
'path' => $CONFIG['fileManagerListPath'],
);
$result = $this->listFile($config);
break;
case 'listimage':
$config = array(
'allowFiles' => $CONFIG['imageManagerAllowFiles'],
'listSize' => $CONFIG['imageManagerListSize'],
'path' => $CONFIG['imageManagerListPath'],
);
$result = $this->listFile($config);
break;
case 'catchimage':
$config = array(
"pathFormat" => $CONFIG['catcherPathFormat'],
"maxSize" => $CONFIG['catcherMaxSize'],
"allowFiles" => $CONFIG['catcherAllowFiles'],
"oriName" => "remote.png"
);
$fieldName = $CONFIG['catcherFieldName'];
$result = $this->saveRemote($config, $fieldName);
break;
default:
$result = json_encode(array(
'state' => 'wrong require'
));
break;
}
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
$this->output = htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
$this->output = json_encode(array(
'state' => 'callback参数不合法'
));
}
} else {
$this->output = $result;
}
}
/**
*
* 输出结果
* @param data 数组数据
* @return 组合后json格式的结果
*/
public function output() {
return $this->output;
}
/**
* 上传文件方法
*/
private function uploadFile($config, $fieldName) {
$upload = new \Think\Upload();
$upload->maxSize = $config['maxSize']; // 设置附件上传大小
$upload->exts = $this->format_exts($config['allowFiles']); // 设置附件上传类型
$upload->rootPath = '.' . $this->rootpath; // 设置附件上传根目录
$upload->autoSub = false;
$uploa