1. 程式人生 > >laravel框架上傳檔案到七牛雲

laravel框架上傳檔案到七牛雲

模型程式碼

 public function uploadFile()
    {
        // 判斷是否有檔案上傳
        if (request()->hasFile('img')) {
            // 獲取檔案,file對應的是前端表單上傳input的name
            $file = request()->file('img');
            // 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);
                echo '上傳成功'.$path;
            }else{
                echo '上傳失敗';
            }

        }else{
            echo '沒有檔案';
        }

檢視頁面

form method="post" action="{{url('upload')}}" enctype="multipart/form-data">
    {{csrf_field()}}
    <input type="file" name="img">
    <button type="submit">上傳圖片</button>
</form>

將檔案App\config\app.php

在陣列prividers中新增
itbdw\QiniuStorage\QiniuFilesystemServiceProvider::class,

將檔案App\config\filesystems.php

向disks陣列中新增一個數組
 'qiniu' => [
            'driver'    => 'qiniu',
            'domain'    => 'xxxxx.bkt.clouddn.com',  //你的七牛域名
            'access_key'=> '',    //AccessKey  askey
            'secret_key'=> '',   //SecretKey   skkey
            'bucket'    => '1803a',    //Bucket名字
        ],

如果上傳較大的檔案需要修改php.ini 中的

最大限制100M

post_max_size = 100M

以及
upload_max_filesize = 100M

 修改php study中的擴充套件檔案中的引數值設定

這裡是修改上傳時間,因為正常上傳檔案超過30秒會失敗,這裡調整大小為防止檔案過大

max_execution_time 中的數值 
以及
max_input_time 中的值