laravel 七牛云文件上传

<?php
namespace App\Cls;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Qiniu
{
	public function __construct()
	{
		$accessKey = env('QINIU_ACCESS_KEY');
        $secretKey = env('QINIU_SECRET_KEY');
        $bucket = env('QINIU_BUCKET');
        $domain = env('QINIU_URL');
        $auth = new Auth($accessKey, $secretKey);
        $this->token = $auth->uploadToken($bucket);
        $this->uploadMgr = new UploadManager();
	}
	//base64上传
	public function upload_pp($baseImage)
	{
		if(strpos($baseImage,';base64') === false){
			$baseImage = 'data:image/png;base64,'.$baseImage;
		}
		$match = preg_match('/^(data:\s*image\/(\w+);base64,)/', $baseImage, $result);
        if (!$match) {
            return ['code'=>0,'msg'=>'不是合规的图片文件'];
        }
        $baseImage = str_replace($result[1], '', $baseImage);
        $baseImage = str_replace(' ', '+', $baseImage);
        $type = $result[2];

		//新文件名
		$key = 'dd/'.date('Ymd') .'/'.guidv4() . '.' . $type;
		list($ret, $err) = $this->uploadMgr->put($this->token, $key, base64_decode($baseImage));
		if ($err !== null) {
			return ['code'=>0,'msg'=>$err];
		}
		$path = 'https://'.env('QINIU_URL'). '/' .$key;
		return ['code'=>1,'data'=>$path];
	}
	//file对象上传
	public function upload_p3($file)
	{
		
		$filePath = $file->getClientOriginalName();
		$fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
		//新文件名
		$key = 'dd/'.date('Ymd') .'/'.guidv4() . '.' . $fileExtension;
		list($ret, $err) = $this->uploadMgr->putFile($this->token, $key, $file);
		if ($err !== null) {
			return ['code'=>0,'msg'=>$err];
		}

		$path = 'https://'.env('QINIU_URL'). '/' .$key;
		return ['code'=>1,'data'=>$path];
	}
}
//公共方法
//生成uuid
function guidv4($data = null) {
    // Generate 16 bytes (128 bits) of random data or use the data passed into the function.
    $data = $data ?? random_bytes(16);
    assert(strlen($data) == 16);

    // Set version to 0100
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
    // Set bits 6-7 to 10
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80);

    // Output the 36 character UUID.
    //return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    $ga = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    return md5($ga);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值