laravel请求文件返回null(添加了enctype)

时间:2018-11-16 03:58:45

标签: laravel laravel-5.7

当我上传任何东西时,它返回null,但是如果我尝试在控制器中返回$ _FILES,它返回我上传的文件,则我添加了enctype =“ multipart / form-data”但仍然没有运气

这是我的表格

<form action="{{url('add_attachments')}}" method="post" enctype="multipart/form-data">
    @csrf @method('put')
    <div class="row">
        <div class="col-12">
            <label class="small">Tambahkan lampiran</label>
            <div class="form-group mb-1 upload">
                <input type="file" name="lampiran[]" multiple accept="image/jpeg,image/gif,image/png,application/pdf" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <button type="submit">Upload</button>
        </div>
    </div>
</form>

这是我的控制人

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

    class ProposalController extends Controller
    {
        public function add_attachment(Request $request, $id)
        {
            return $request->file('lampiran'); //return null
            return $_FILES; //return {"lampiran":{"name":["7680cf333da548d4213fd5f574b66ffbdccba917.jpg"],"type":["image\/jpeg"],"tmp_name":["\/tmp\/phpBIcJYs"],"error":[0],"size":[138198]}}
        }
    }

1 个答案:

答案 0 :(得分:0)

您的<input type="file" name="lampiran[]" multiple accept="image/jpeg,image/gif,image/png,application/pdf"/>是一个数组 lampiran []

尝试

public function add_attachment(Request $request,$id)
{
    return $request->file('lampiran.0');
}

public function add_attachment(Request $request,$id)
{
    foreach($request->file('lampiran') as $image){
         dd($image):
    }
}