<?php
/**
- Created by PhpStorm.
- User: admin
- Date: 2018/10/10
- Time: 20:32
*/
namespace app\common\lib;
use Qiniu\Auth; //引用鉴权类
use Qiniu\Storage\UploadManager;
use think\Controller; //应用上传类
/**
- 基于七牛图图片上传基类
- Class Upload
- @package app\common\lib
/
class Upload extends Controller
{
/*-
图片异常
*/
public static function image()
{if (empty($_FILES[‘file’][‘tem_name’])) {
exception(‘提交图片不合法’, 404);
}// 要上传的临时文件名
$filePath = $_FILES[‘file’][‘tmp_name’];
$ext = explode(’.’, $_FILES[‘file’][‘name’]);
//pathinfo ( $_File[‘file’][‘name’]); 获取扩展名$ext = $ext[1];
/*
这是配置里面的参数
*return [
‘ak’=>‘xxxx’,
‘sk’=>‘xxxx’,
‘bucket’=>‘shop’,
‘image_url’=>‘七牛云的地址’
*/
//构建一个鉴权$config = config(‘qiniu’); //获取配置里面参数
//构建一个鉴权对象
config[‘ak’], $config[‘sk’]);
// 生成上传的token
$token = config[‘bucket’]);
// 上传到七牛的按年月日文件名
filePath), 0, 6)
. date(“YmdHis”) . rand(0, 100000) . ‘.’ . $ext;//初始化uploadMananger 类
/**- 上传文件到七牛
- @param $upToken 上传凭证
- @param $key 上传文件名
- @param $filePath 上传文件的路径
- @param $params 自定义变量,规格参考
-
http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
- @param $mime 上传数据的mimeType
- @param $checkCrc 是否校验crc32
- @return array 包含已上传文件的信息,类似:
-
[
-
"hash" => "<Hash string>",
-
"key" => "<Key string>"
-
]
- */
ret,$error) = token, filePath);
if ($error){
return null;
}else{
return $key;
}
}
}
-