首先去七牛云官网下载phpSDK工具放在Think/library/Vendor下。
ueditor后台调用方法:/**
* 上传文件
* @param
*/
public function UploadSomething(){
header("Content-Type: text/html; charset=utf-8");
error_reporting(E_ERROR);
// 登录检测
if($this->uid == 0){
if($_GET['action'] == 'config'){
echo preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./Public/js/php/config.json"));
}else{
echo json_encode(array('state'=> '请登录!'));
}
exit;
}
echo conditionChoice($this->uid);
}
放在ThinkPHP的Common.php中:/**
* 条件选择
* @param
*/
function conditionChoice($uid){
$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./Public/js/php/config.json")), true);
$action = $_GET['action'];
$Qiniu = C('QINIU');
$domain = $Qiniu['domain'];
switch ($action){
case 'config':
$result = json_encode($CONFIG);
break;
case 'uploadimage':
case 'uploadscrawl':
case 'uploadvideo':
case 'uploadfile':
$result = Upload($CONFIG,$uid);
// 加上公网引用地址
$result = json_decode($result,JSON_UNESCAPED_UNICODE);
if(strtolower(strrchr($result['url'],'.')) != '.gif'){
$result['url'] = $domain.$result['url'].'_600.jpg';
}else{
$result['url'] = $domain.$result['url'];
}
$result = json_encode($result,JSON_UNESCAPED_UNICODE);
break;
case 'listimage':
$result = Lists($uid);
break;
case 'listfile':
$result = json_encode(array('state'=> 'error 1000'));
break;
case 'catchimage':
$result = json_encode(array('state'=> 'error 1001'));
break;
default:
$result = json_encode(array(
'state'=> '请求地址出错'
));
break;
}
if(isset($_GET["callback"])){
if(preg_match("/^[\w_]+$/", $_GET["callback"])){
return htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
}else{
return json_encode(array('state'=> 'callback参数不合法'));
}
}else{
return $result;
}
}
/**
* 获取文件列表
* @param
*/
function Lists($uid){
$limit = 40;
$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $limit;
$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
$end = $start + $size;
$list = getfiles($start,$limit,$uid);
if (!count($list)){
return json_encode(array(
"state" => "no match file",
"list" => array(),
"start" => $start,
"total" => count($list)
));
}
$result = json_encode(array(
"state" => "SUCCESS",
"list" => $list,
"start" => $start,
"total" => count($list)
));
return $result;
}
/**
* 获取指定文件列表
* @param int $start 开始位置
* @param int $limit 显示的条数
* @param int $uid用户id
*/
function getfiles($start,$limit,$uid){
Vendor('Qiniu.autoload');
$Qiniu = C('QINIU');
$accessKey= $Qiniu['accessKey'];
$secretKey= $Qiniu['secretKey'];
$bucket= $Qiniu['bucket'];
$domain= $Qiniu['domain'];
$auth = new \Qiniu\Auth($accessKey, $secretKey);
$bucketMgr = new \Qiniu\Storage\BucketManager($auth);
// 要列取文件的公共前缀
if($uid){
$prefix = 'test/'.$uid.'/';
}else{
return array();
}
// 上次列举返回的位置标记,作为本次列举的起点信息。
$marker = $start;
// 本次列举的条目数
$limit = $limit ? $limit : 0;
// 列举文件
list($iterms, $marker, $err) = $bucketMgr->listFiles($bucket, $prefix, $marker, $limit);
if ($err !== null){
return array();
}else{
$arr = array();
for ($i = 0; $i
if(strtolower(strrchr($iterms[$i]['key'], '.')) == '.gif'){
$arr[$i]['url'] = $domain.$iterms[$i]['key'];
}else{
$arr[$i]['url'] = $domain.$iterms[$i]['key'].'_600.jpg';
}
$arr[$i]['mtime'] = $iterms[$i]['putTime'];
}
return $arr;
}
}
/**
* 上传文件
* @param
*/
function Upload($CONFIG,$uid){
if(!$uid){
return json_encode(array("state" => 'error 1002'));
}
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
case 'uploadimage':
$config = array(
"pathFormat" => $CONFIG['imagePathFormat'],
"maxSize" => $CONFIG['imageMaxSize'],
"allowFiles" => $CONFIG['imageAllowFiles']
);
$fieldName = $CONFIG['imageFieldName'];
break;
case 'uploadscrawl':
$config = array(
"pathFormat" => $CONFIG['scrawlPathFormat'],
"maxSize" => $CONFIG['scrawlMaxSize'],
"allowFiles" => $CONFIG['scrawlAllowFiles'],
"oriName" => "scrawl.png"
);
$fieldName = $CONFIG['scrawlFieldName'];
$base64 = "base64";
break;
case 'uploadvideo':
$config = array(
"pathFormat" => $CONFIG['videoPathFormat'],
"maxSize" => $CONFIG['videoMaxSize'],
"allowFiles" => $CONFIG['videoAllowFiles']
);
$fieldName = $CONFIG['videoFieldName'];
break;
case 'uploadfile':
default:
$config = array(
"pathFormat" => $CONFIG['filePathFormat'],
"maxSize" => $CONFIG['fileMaxSize'],
"allowFiles" => $CONFIG['fileAllowFiles']
);
$fieldName = $CONFIG['fileFieldName'];
break;
}
$prefix = 'test/'.$uid.'/';
$key = $prefix.time();
return json_encode(UpCondition($fieldName,$config,$key));
}
/**
* 文件判断
* @param string $fileField表单名称
* @param config $config 配置文件
* @param config $key新文件名称
*/
function UpCondition($fileField,$config,$key){
$file = $_FILES[$fileField];
if (!$file){
return array("state" => '找不到上传文件');
}
if ($file['error']){
return array("state" => $file['error']);
} else if (!file_exists($file['tmp_name'])) {
return array("state" => '找不到临时文件');
} else if (!is_uploaded_file($file['tmp_name'])) {
return array("state" => '不是上传文件');
}
// 文件后缀
$ext = strtolower(strrchr($file['name'], '.'));
//检查文件大小是否超出限制
if($file['file'] > $config["maxSize"]){
return array("state" => '文件过大');
}
//检查是否不允许的文件格式
if(!in_array($ext, $config["allowFiles"])){
return array("state" => '文件格式不允许');
}
$url= $key.$ext;
$title= substr($url, strrpos($url, '/') + 1);
//移动文件
$ret = QiniuUpload($file["tmp_name"],$url);
if($ret['status'] == 1){
return array(
"state" => 'SUCCESS',
'url'=> $url,
'title'=> $title,
'original' => $file['name'],
'type'=> $ext,
'size'=> $file['size'],
);
}else{
return array("state" => '上传失败');
}
}
/**
* 上传文件
* @param $filePath要移动的文件位置
* @param $key新文件名称
*/
function QiniuUpload($filePath,$key){
Vendor('Qiniu.autoload');
$Qiniu = C('QINIU');
$accessKey= $Qiniu['accessKey'];
$secretKey= $Qiniu['secretKey'];
$bucket= $Qiniu['bucket'];
// 初始化签权对象
$auth = new \Qiniu\Auth($accessKey, $secretKey);
$token = $auth->uploadToken($bucket);
$uploadMgr = new \Qiniu\Storage\UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
if($err !== null){
$err['status'] = 0;
return $err;
}else{
$ret['status'] = 1;
return $ret;
}
}