3.2 qiniu cloud storage file

Source: Internet
Author: User
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn how to use qiniu cloud private space to store files.
Step 1: Register qiniu cloud, create a space, and set the space as private
Something to note:
AK, SK, and bucket
Step 2 configure ThinkPHP
Add in config. php 'Upload _ SITEIMG_QINIU '=> array (
'Maxsize' => 5*1024*1024, // File Size
'Rootpath' => './',
'Savename' => array ('uniqid ',''),
'Driver '=> 'qiniu ',
'Driverconfig' => array (
'Secretkey' =>' <这里填七牛sk> ',
'Accesskey' =>' <这里填七牛ak> ',
'Domain '=>' <空间名称> .Qiniudn.com ',
'Bucket' =>' <空间名称> ',
)
Step 3: upload files
Controller $setting=C('UPLOAD_SITEIMG_QINIU');
$Upload = new \Think\Upload($setting);
$info = $Upload->upload($_FILES);
The $ info obtained after the file is uploaded is as follows: array(1) {
["photo"] => array(10) {
["name"] =>
["type"] =>
["size"] =>
["key"] =>"
["ext"] =>
["sha1"] =>
["savename"] =>
["savepath"] =>
["url"] =>
}
}
Step 4: Calculate the download credential
Common directory function. php add Method Function Qiniu_Encode ($ str) // URLSafeBase64Encode
{
$ Find = array ('+ ','/');
$ Replace = array ('-','_');
Return str_replace ($ find, $ replace, base64_encode ($ str ));
}
Function Qiniu_Sign ($ url) {// url in $ info
$ Setting = C ('upload _ SITEIMG_QINIU ');
$ Duetime = NOW_TIME + 86400; // validity period of the downloaded credential
$ DownloadUrl = $ url .'? E = '. $ duetime;
$ Sign = hash_hmac ('sha1', $ DownloadUrl, $ setting ["driverConfig"] ["secrectKey"], true );
$ EncodedSign = Qiniu_Encode ($ Sign );
$ Token = $ setting ["driverConfig"] ["accessKey"]. ':'. $ EncodedSign;
$ RealDownloadUrl = $ DownloadUrl. '& token ='. $ Token;
Return $ RealDownloadUrl;
}
$ RealDownloadUrl: the available URL for downloading the corresponding private resource
Reference
Http://document.thinkphp1.cn/manual_3_2.html#upload
Http://developer.qiniu.com/docs/v6/api/reference/security/download-token.html




AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.