Home >php教程 >php手册 >thinkphp5+zyFile.js uploads pictures to Qiniu Cloud

thinkphp5+zyFile.js uploads pictures to Qiniu Cloud

WBOY
WBOYOriginal
2016-08-10 08:48:592384browse

thinkphp uploads pictures to Qiniu Cloud without refreshing through zyfile, and obtains file information in the storage space under Qiniu Cloud
Previously, we released a method for uploading pictures to the local through zyFile. Of course, we often use cnd when making websites. So, I will share with you how to upload pictures to Qiniu Cloud.
I would like to thank Qiniuyun here (no money required, haha).
Let’s talk about the basic configuration first. First configure the keys under application/index/config. These keys can be viewed in the Qiniu Cloud Personal Center-->Key Management. The BUCKET parameter is the name of the storage space you want to upload. . Well, basically there is no need to change anything.

The code of index.php is as follows: <?php<br /> // +-------------------------------------------------- -----------------------<br /> // | snake<br /> // +-------------------------------------------------- -----------------------<br /> // | Copyright (c) 2016~2022 http://baiyf.com All rights reserved.<br /> // +-------------------------------------------------- -----------------------<br /> // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )<br /> // +-------------------------------------------------- -----------------------<br /> // | Author: NickBai <1902822973@qq.com><br> // +-------------------------------------------------- -----------------------<br> namespace appindexcontroller;<br> <br> use thinkController;<br> use QiniuAuth;<br> use QiniuStorageBucketManager;<br> use QiniuStorageUploadManager;<br> <br> class Index extends Controller<br> {<br> Public function index()<br> {<br>            return $this->fetch('/index');<br> }<br> <br> /**<br> * *Default home page in the background<br> * * @return mixed<br> ​​*/<br> Public function indexPage()<br> {<br>           return $this->fetch('index');<br> }<br> <br> /**<br> * *All pictures under cloud storage<br> * * @return mixed<br> ​​*/<br> Public function picList()<br> {<br>                                                                                                                                                                                                                        . <br> ​​​​$accessKey = config('ACCESSKEY');<br>         $secretKey = config('SECRETKEY');<br> <br>         $auth = new Auth($accessKey, $secretKey);<br>          $bucketMgr = new BucketManager($auth);<br> <br> ​​​​​//The name of the space to be listed<br>         $bucket = config('BUCKET');<br> ​​​​//To list the public prefix of the file<br>          $prefix =                                                                             // The position mark returned by the last enumeration is used as the starting point information of this enumeration. <br>          $marker =                                                              ​​​​//The number of items listed this time<br>             //$limit = 3;<br> <br>                 // List files<br>          $list = $bucketMgr->listFiles($bucket, $prefix, $marker);<br>          $list = array_filter($list);<br> <br>           $this->assign([<br>              'list' => $list<br> ]);<br> <br>           return $this->fetch();<br> }<br> <br> //Upload pictures <br> Public function showUpload()<br> {<br>           return $this->fetch();<br> }<br> <br> <br>//Main method for processing upload<br> Public function upload()<br> {<br>           $file = request()->file('fileList');<br> <br>                                                                                                                                                                                                      require_once APP_PATH <br> ​​​​//You need to fill in your Access Key and Secret Key<br> ​​​​$accessKey = config('ACCESSKEY');<br>         $secretKey = config('SECRETKEY');<br>                   // Construct authentication object <br>         $auth = new Auth($accessKey, $secretKey);<br> <br>              // Space to upload<br>          $bucket = config('BUCKET');<br> <br>                  // Generate and upload Token<br>         $token = $auth->uploadToken($bucket);<br> <br>                // Local path to upload the file<br>          $filePath = $file->getRealPath();<br> <br>          $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //Suffix<br> ​​​<br> ​​​​//The file name saved after uploading to Qiniu<br>         $key = substr(md5($file->getRealPath()), 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;<br> <br>               // Initialize the UploadManager object and upload files<br>         $uploadMgr = new UploadManager();<br> <br> ​​​​//Call the putFile method of UploadManager to upload files<br> list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);<br> If ($err !== null) {<br>             var_dump($err);<br>            } else {<br> echo $bucket . '/' . $key;<br>         }<br> }<br> }<br>There are quite a lot of files under view, so I won’t list the codes here. Just download them and check them out nearby. This example is just a simple demonstration. Of course, many functions are not perfect. Please don’t complain. It just provides an implementation idea for people who want to use this.
Qiniu Cloud's SDK can be updated through composer, and the demo inside can meet all your needs. You only need to change it to tp5 writing like me.

thinkphp5+zyFile.js uploads pictures to Qiniu Cloud
thinkphp5+zyFile.js uploads pictures to Qiniu Cloud
Okay, the file size is too large! Then I will put the code into the cloud disk. If you are interested, you can go and take a look
http://pan.baidu.com/s/1bp5etbh
My github: https://github.com/nick-bai/


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