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