PHP上傳檔案到七牛(Qiniu)

PMPSSPMP發表於2019-02-16

上傳檔案到七牛最簡單的方式就是使用七牛官方最新的SDK

安裝PHP SDK

composer require qiniu/php-sdk

上傳檔案到七牛

use QiniuAuth;
use QiniuStorageUploadManager;

$cfg = [
  `access` => `YOUR_ACCESS_KEY`,
  `secret` => `YOUR_SECRET_KEY`,
  `bucket` => `YOUR_BUCKET`,
  `domain` => `https://images.your_domain.com`
];

$auth = new Auth($cfg[`access`], $cfg[`secret`]);
// 建立一個過期時間為1小時的臨時上傳令牌
$token = $auth->uploadToken($cfg[`bucket`], null, 3600);

$filePath = "./illustration.png";

$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, null, $filePath);
if($err !== null) {
    $this->err = $err;
} else {
    echo $cfg[`domain`] . `/` . $ret[`key`];
}

搞定!

相關文章