Laravel 5.1: How to upload multiple files from three different file input fields?

后端 未结 1 1351
心在旅途
心在旅途 2021-01-22 15:23

I have a form in which user should at least select one file to be uploaded. I have three file input fields like this:

            
1条回答
  •  Happy的楠姐
    2021-01-22 15:49

    After some searching around I finally came up with a solution. First of all I modified the view to look like this:

    {!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!} {!! Form::file('file1', ['id'=>'file1']) !!}
    {!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!} {!! Form::file('file2', ['id'=>'file2']) !!}
    {!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!} {!! Form::file('file3', ['id'=>'file3']) !!}

    Then in the controller I used your suggested code:

    $files =[];
            if ($request->file('file1')) $files[] = $request->file('file1');
            if ($request->file('file2')) $files[] = $request->file('file2');
            if ($request->file('file3')) $files[] = $request->file('file3');
            foreach ($files as $file)
            {
                if(!empty($file)){
                    $filename=$file->getClientOriginalName();
                    $file->move(
                        base_path().'/public/uploads/', $filename
                    );
                }
    
            }
    

    0 讨论(0)
提交回复
热议问题