码迷,mamicode.com
首页 > Web开发 > 详细

上传图片到腾讯云

时间:2018-11-10 15:07:37      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:col   time   ram   test   upload   root   函数   http   div   

1、composer安装扩展

"qcloud/cos-sdk-v5": "1.*"

2、代码,多图

<?php

namespace App\Library\lib;

use Qcloud\Cos\Client;

class CosLib
{
    const REGION = test;
    const APP_ID = test;
    const SECRET_ID = test;
    const SECRET_KEY = test;

    const BUCKET = test;
    const ROOT_PATH = test;
    /**
     * 【构造函数】
     *
     */
    public function __construct() {

    }

    /**
     * 上传多张图片到腾讯云
     * @param $files
     * $files示例:$request->file(‘file_img‘)
     * @return array
     */
    public static function uploadSkuImg($files) {

        $cosClient = new Client(
            array(
                region => self::REGION,
                credentials=> array(
                    appId => self::APP_ID,
                    secretId    => self::SECRET_ID,
                    secretKey => self::SECRET_KEY
                )
            )
        );
        $httpPaths = [];
        try {
            foreach ($files as $file) {
                $realPath= $file->getRealPath();

                $currentTime = time();
                $curY = date(Y, $currentTime);
                $curM = date(m, $currentTime);
                $curD = date(d,$currentTime);
                $fileName = $curY.$curM.$curD.$currentTime.mt_rand(1000, 9999)..jpg;
                $key = $curY./.$curM./.$curD./.$fileName;

                $result = $cosClient->putObject(
                    array(
                        Bucket => self::BUCKET,
                        Key =>  $key,
                        Body => file_get_contents($realPath)
                    )
                );

                $httpPaths[] = self::ROOT_PATH . $key;
            }
        } catch (\Exception $e) {
            return [
                code => 201,
                result => $e->getMessage()
            ];
        }

        return [
            code => 200,
            result => $httpPaths
        ];
    }
}

 

上传图片到腾讯云

标签:col   time   ram   test   upload   root   函数   http   div   

原文地址:https://www.cnblogs.com/zhengchuzhou/p/9939044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!