thinkphp5.1调用七牛云SDK上传文件

 

 

 

thinkphp5.0

class Upload 
{
    
    public static function image(){
        if(empty($_FILES['file']['tmp_name'])){
            exception('你提交的图片数据不合法',404);
        }
        // 图片的本地路径
        $file = $_FILES['file']['tmp_name'];
        // 换取上传图片的后缀
        // $ext = explode('.',$_FILES['file']['name'])[1];
        $pathinfo = pathinfo($_FILES['file']['name']);
        $ext = $pathinfo['extension'];
        $config = config('qiniu');
        // 构建一个鉴权对象
        $auth = new Auth($config['ak'],$config['sk']);
        // 生成上传的token
        $token = $auth->uploadToken($config['bucket']);
        // 上传到七牛后保存的文件名
        $key = date('Y').'/'.date('m').'/'.substr(md5($file),0,5).date('YmdHis').mt_rand(0,9999).'.'.$ext;
 
        // 初始化UploadManager类
        $uploadMgr = new UploadManager();
        list($ret,$err) = $uploadMgr->putFile($token,$key,$file);
        if($err !== null){
            return null;
        }else{
            return $key;
        }
    }

 

posted @ 2019-06-26 10:39  搬砖小伙子  阅读(1372)  评论(0编辑  收藏  举报