计划用php写一个七牛文件上传小工具
生活随笔
收集整理的這篇文章主要介紹了
计划用php写一个七牛文件上传小工具
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
功能
輸入框中填入:bucket ? Access Key 和 Secret Key?
點(diǎn)擊提交后,向七牛服務(wù)器認(rèn)證,若認(rèn)證通過,則在當(dāng)前頁面動(dòng)態(tài)刷新,剛才的輸入框消失。
顯示一個(gè)上傳文件按鈕,點(diǎn)擊可繼續(xù)添加文件(最多同時(shí)上傳5個(gè))。文件的key為yyy-mm-dd-hh-name.xxx
然后提交文件后顯示進(jìn)度條,并在下面動(dòng)態(tài)回顯上傳文件的結(jié)果以及文件的url。
七牛API
上傳策略
{"scope":???????????????"<Bucket???????????????????string>","deadline":?????????????<UnixTimestamp????????????int64>,"endUser":?????????????"<EndUserId????????????????string>","returnUrl":???????????"<RedirectURL??????????????string>","returnBody":??????????"<ResponseBodyForAppClient?string>","callbackBody":????????"<RequestBodyForAppServer??string>","callbackUrl":?????????"<RequestUrlForAppServer???string>","persistentOps":???????"<persistentOpsCmds????????string>","persistentNotifyUrl":?"<persistentNotifyUrl??????string>","insertOnly":??????????"<AllowFileUpdating????????uint16>","detectMime":??????????"<AutoDetectMimeType???????uint16>","fsizeLimit":??????????"<FileSizeLimit????????????int64>","saveKey":?????????????"<KeyFomart????????????????string>","mimeLimit":???????????"<MimeLimit????????????????string>" }其中scope和deadline為必選。
上傳憑證
1.構(gòu)造上傳策略 2.將上傳策略序列化成為JSON格式 3.對(duì)JSON編碼的上傳策略進(jìn)行URL安全的Base64編碼,得到待簽名字符串: encodedPutPolicy?=?urlsafe_base64_encode(putPolicy) 4.使用SecertKey對(duì)上一步生成的待簽名字符串計(jì)算HMAC-SHA1簽名 sign?=?hmac_sha1(signingStr,?"<SecretKey>") 5.對(duì)簽名進(jìn)行URL安全的Base64編碼: encodedSign?=?urlsafe_base64_encode(sign) 6.將AccessKey、encodedSign和encodedPutPolicy用:連接起來: uploadToken?=?AccessKey?+?':'?+?encodedSign?+?':'?+?encodedPutPolicy表單上傳(POST方式)
<form?method="post"?action="http://up.qiniu.com/"enctype="multipart/form-data"><input?name="key"?type="hidden"?value="<resource_key>"><input?name="x:<custom_name>"?type="hidden"?value="<custom_value>"><input?name="token"?type="hidden"?value="<upload_token>"><input?name="file"?type="file"?/> </form>
token和file為必選,token指的是上傳憑證,file為文件本身
反饋
PHP實(shí)現(xiàn)
服務(wù)端生成上傳憑證
require_once("qiniu/rs.php");$bucket?=?'phpsdk'; $accessKey?=?'<YOUR_APP_ACCESS_KEY>'; $secretKey?=?'<YOUR_APP_SECRET_KEY>';Qiniu_SetKeys($accessKey,?$secretKey); $putPolicy?=?new?Qiniu_RS_PutPolicy($bucket); $upToken?=?$putPolicy->Token(null);
上傳本地文件
require_once("qiniu/io.php"); require_once("qiniu/rs.php");$bucket?=?"phpsdk"; $key1?=?"file_name1"; $accessKey?=?'<YOUR_APP_ACCESS_KEY>'; $secretKey?=?'<YOUR_APP_SECRET_KEY>';Qiniu_SetKeys($accessKey,?$secretKey); $putPolicy?=?new?Qiniu_RS_PutPolicy($bucket); $upToken?=?$putPolicy->Token(null); $putExtra?=?new?Qiniu_PutExtra(); $putExtra->Crc32?=?1; list($ret,?$err)?=?Qiniu_PutFile($upToken,?$key1,?__file__,?$putExtra); echo?"====>?Qiniu_PutFile?result:?\n"; if?($err?!==?null)?{var_dump($err); }?else?{var_dump($ret); }
參考
[1]:http://developer.qiniu.com/docs/v6/api/reference/security/upload-token.html
[2]:http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
轉(zhuǎn)載于:https://my.oschina.net/itfanr/blog/195278
總結(jié)
以上是生活随笔為你收集整理的计划用php写一个七牛文件上传小工具的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 购标网群公告
- 下一篇: PHP实现文件安全下载