//首先下載好七牛的PHPSDK放到VENDOR目錄下,下面在引入VENDOR下的autoload.php時會自動加載所有文件
public function upload(){
if(Request::instance()->isPost()){
//引入vendor下的七牛SDK
vendor('Qiniu.autoload');
$ak = config('QI_NIU_ACCESSKEY');
$sk = config('QI_NIU_SECRETKEY');
//獲取HTML頁面提交過來的name="image"的圖片文件
$file = \request()->file('image');
$filePath = $file->getRealPath();
$ext = pathinfo($file->getInfo('name'),PATHINFO_EXTENSION);
$key = substr(md5($file->getRealPath()),0,5).date('YmdHis').rand(0,9999).'.'.$ext;
$auth = new \Qiniu\Auth($ak,$sk);
$bucket = config('QI_NIU_BUCKET');
$domain = config('QI_NIU_DOMAIN');
//獲取圖片上傳憑證
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
//圖片上傳
list($ret,$err) = $uploadMgr->putFile($token,$key,$filePath);
if($err !== null){
echo ['err'=>1,'msg'=>$err,'data'=>''];
}else{
var_dump($ret);
//正常情況下會返回下面這種數組,KEY則是文件名或圖片文件名.
// 'hash' => string 'FqUNBBH7VP_e3qTDhrn0AXTKrYJj' (length=28)
//'key' => string '44a34201808201848142633.pdf' (length=27)
}
}else{
return $this->fetch('image');
}