Store files with seven Qiniu private spaces
The first step is to register the seven Cow cloud, create space, and set the space as private
Something to write down:
Ak,sk,bucket
Second Step configuration thinkphp
Add in config.php
' Upload_siteimg_qiniu ' =>array (' maxSize ' =>5*1024*1024,//file size ' rootpath ' = './', ' Savename ' =>array (' Uniqid ', '), ' driver ' = ' qiniu ', ' Driverconfig ' =>array (' secrectkey ' = ' < here fill seven kn sk> ', ' accessKey ' = ') < here fill seven kn ak> ', ' domain ' = ' < space name >.qiniudn.com ', ' bucket ' = ' < space name > ', '
Copy Code
Step three, upload the file
Controller
$setting =c (' Upload_siteimg_qiniu '); $Upload =new\think\upload ($setting); $info = $Upload->upload ($_files);
Copy Code
The $info you get when uploading a file is this.
Array (1) {["Photo"]=>array (Ten) {["Name"]=>["type"]=>["size"]=>["key"]=> "[" ext "]=>[" SHA1 "]=> ["Savename"]=>["Savepath"]=>["url"]=>}}
Copy Code
Fourth step, calculate download voucher
Common directory function.php Add method
Functionqiniu_encode ($STR)//urlsafebase64encode{$find =array (' + ', '/'); $replace =array ('-', ' _ '); returnstr_replace ($find, $replace, Base64_encode ($STR));} Functionqiniu_sign ($url) {//$info inside of Url$setting=c (' Upload_siteimg_qiniu '); $duetime =now_time+86400;//Download voucher valid time $ 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;}
Copy Code
$RealDownloadUrl to download the available URLs for the corresponding private resource
Reference
Http://document.thinkphp.cn/manual_3_2.html#upload
Http://developer.qiniu.com/docs/v6/api/reference/security/download-token.html
Thinkphp using seven cow cloud upload