Home >Backend Development >PHP Tutorial >PHP method code for uploading files to Qiniu
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!