在Laravel 5中上传多个文件

时间:2018-03-07 14:34:51

标签: laravel

我一直在尝试使用下面的代码在Laravel上传多个文件,但它只上传一个图像。请帮忙

$ files = $ request-> file('file');

    foreach ($files as $file){

        $filename = time().'.'.$file->getClientOriginalExtension();

        $location = public_path('uploads/'.$filename);

        $file->move(public_path().'/uploads/', $filename);

        $filename_arr = [];
        array_push($filename_arr, $filename);
        $filename = json_encode($filename_arr);
        $upload->filename = $filename;
    }

1 个答案:

答案 0 :(得分:1)

Blade:由于您要将多个文件append []上传到输入类型名称属性,其中包含多个,如下所示:

<input type="file" name="file[]" multiple>

逻辑:

if($request->hasFile('file'))
{
    $files = $request->file('file');
    foreach ($files as $file) {
        $filename = $file->getClientOriginalName();
        $file->move(public_path().'/uploads, $filename);
    }
}