laravel 七牛檔案上傳
阿新 • • 發佈:2018-11-30
安裝Laravel七牛擴充套件包
過Composer安裝
composer require zgldh/qiniu-laravel-storage
接下來在 config/filesystems.php
裡的disks中新增七牛配置:
'qiniu' => [ 'driver' => 'qiniu', 'domains' => [ 'default' => 'xxxxx', //你的七牛域名 'https' => 'xxxxx', //你的HTTPS域名 'custom' => 'xxxxx', //你的自定義域名 ], 'access_key'=> '', //AccessKey 'secret_key'=> '', //SecretKey 'bucket' => '', //Bucket名字 'notify_url'=> '', //持久化處理回撥地址 ],
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use zgldh\QiniuStorage\QiniuStorage; class UploadController extends Controller { /** * 上傳檔案到七牛 * @author 高偉 * @date 2016-11-09T16:58:37+0800 * @param Request $request [description] * @return [type] [description] */ public function uploadFile(Request $request) { // 判斷是否有檔案上傳 if ($request->hasFile('file')) { // 獲取檔案,file對應的是前端表單上傳input的name $file = $request->file('file'); // Laravel5.3中多了一個寫法 // $file = $request->file; // 初始化 $disk = QiniuStorage::disk('qiniu'); // 重新命名檔案 $fileName = md5($file->getClientOriginalName().time().rand()).'.'.$file->getClientOriginalExtension(); // 上傳到七牛 $bool = $disk->put('iwanli/image_'.$fileName,file_get_contents($file->getRealPath())); // 判斷是否上傳成功 if ($bool) { $path = $disk->downloadUrl('iwanli/image_'.$fileName); return '上傳成功,圖片url:'.$path; } return '上傳失敗'; } return '沒有檔案'; } }