利用七牛云私有空间存储文件
第一步,注册七牛云,创建空间,将空间设为私有
需要记下的东西:
AK,SK,bucket
第二步配置ThinkPHP
在config.php添加'UPLOAD_SITEIMG_QINIU' => array ( <br>
'maxSize' => 5 * 1024 * 1024,//文件大小<br>
'rootPath' => './',<br>
'saveName' => array ('uniqid', ''),<br>
'driver' => 'Qiniu',<br>
'driverConfig' => array (<br>
'secrectKey' => '', <br>
'accessKey' => '',<br>
'domain' => '.qiniudn.com',<br>
'bucket' => '', <br>
)
第三步,上传文件
控制器$setting=C('UPLOAD_SITEIMG_QINIU');<br>
$Upload = new \Think\Upload($setting);<br>
$info = $Upload->upload($_FILES);
上传文件后得到的$info是这样的 array(1) {<br>
["photo"] => array(10) {<br>
["name"] =><br>
["type"] =><br>
["size"] =><br>
["key"] =>"<br>
["ext"] =><br>
["sha1"] => <br>
["savename"] => <br>
["savepath"] => <br>
["url"] => <br>
}<br>
}
第四步,计算下载凭证
Common目录function.php添加方法function Qiniu_Encode($str) // URLSafeBase64Encode<br>
{<br>
$find = array('+', '/');<br>
$replace = array('-', '_');<br>
return str_replace($find, $replace, base64_encode($str));<br>
}<br>
function Qiniu_Sign($url) {//$info里面的url<br>
$setting = C ( 'UPLOAD_SITEIMG_QINIU' );<br>
$duetime = NOW_TIME + 86400;//下载凭证有效时间<br>
$DownloadUrl = $url . '?e=' . $duetime;<br>
$Sign = hash_hmac ( 'sha1', $DownloadUrl, $setting ["driverConfig"] ["secrectKey"], true );<br>
$EncodedSign = Qiniu_Encode ( $Sign );<br>
$Token = $setting ["driverConfig"] ["accessKey"] . ':' . $EncodedSign;<br>
$RealDownloadUrl = $DownloadUrl . '&token=' . $Token;<br>
return $RealDownloadUrl;<br>
}
$RealDownloadUrl为下载对应私有资源的可用URL
参考
http://document.thinkphp.cn/manual_3_2.html#upload
http://developer.qiniu.com/docs/v6/api/reference/security/download-token.html
AD:真正免费,域名+虚机+企业邮箱=0元