一、composer安裝擴展php
"qcloud/cos-sdk-v5": "1.*"
二、代碼,多圖app
<?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 ]; } }