基于七牛图上传


<?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’); //获取配置里面参数
      //构建一个鉴权对象
      a u t h = n e w A u t h ( auth = new Auth( config[‘ak’], $config[‘sk’]);
      // 生成上传的token
      $token = a u t h &gt; u p l o a d T o k e n ( auth-&gt;uploadToken( config[‘bucket’]);
      // 上传到七牛的按年月日文件名
      k e y = d a t e ( Y ) . / . d a t e ( m ) . / . d a t e ( d ) . / . s u b s t r ( m d 5 ( key = date(&#x27;Y&#x27;) . &#x27;/&#x27; . date(&#x27;m&#x27;) . &#x27;/&#x27; . date(&#x27;d&#x27;) . &#x27;/&#x27; . substr(md5( 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>"
        
      •                                          ]
        
      • */
        u p l o a d M a n a m g e r = n e w U p l o a d M a n a g e r ( ) ; l i s t ( uploadManamger = new UploadManager(); list( ret,$error) = u p l o a d M a n a m g e r &gt; p u t F i l e ( uploadManamger-&gt;putFile( token, k e y , key, filePath);
        if ($error){
        return null;
        }else{
        return $key;
        }
        }
        }

猜你喜欢

转载自blog.csdn.net/qq_33113113/article/details/83046441