Home  >  Article  >  Backend Development  >  PHP method code for uploading files to Qiniu

PHP method code for uploading files to Qiniu

不言
不言Original
2018-07-25 09:51:542246browse

The content shared with you in this article is about the method code of how to upload files to Qiniu in PHP. The content is very detailed. Next, let’s take a look at the specific content. I hope it can help everyone.

The easiest way to upload files to Qiniu is to use the latest official SDK of Qiniu

Install PHP SDK

composer require qiniu/php-sdk

Upload files to Qiniu

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

$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'];
}

Get it done!

Related recommendations:

How to use PHP to implement the download function

PHP code for how to upload images to the database for display

The above is the detailed content of PHP method code for uploading files to Qiniu. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn