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

PHP method code for uploading files to Qiniu

不言
Release: 2023-04-03 12:02:02
Original
2359 people have browsed it

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
Copy after login

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'];
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template