Laravel 5.1:如何从三个不同的文件输入字段上传多个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31936336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:09:35  来源:igfitidea点击:

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

phplaravelfile-uploadlaravel-5multiple-file-upload

提问by Ali Erfani

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

我有一个表单,用户应该至少选择一个要上传的文件。我有三个文件输入字段,如下所示:

            <div class="form-group col-lg-4">
                {!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!}
                {!! Form::file('files[]', ['id'=>'file1']) !!}
            </div>
            <div class="form-group col-lg-4">
                {!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!}
                {!! Form::file('files[]', ['id'=>'file2']) !!}
            </div>
            <div class="form-group col-lg-4">
                {!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!}
                {!! Form::file('files[]', ['id'=>'file3']) !!}
            </div>

I should validate the presence of at least one file and the mime types in a form request. Then in the store method of the related form controller, the original file names should be stored in the three corresponding database fields(namely file1, file2, file3).

我应该验证表单请求中至少存在一个文件和 mime 类型。然后在相关表单控制器的store方法中,原始文件名应该存储在对应的三个数据库字段中(即file1、file2、file3)。

How can I implement this?

我该如何实施?

回答by Ali Erfani

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

经过一番搜索,我终于想出了一个解决方案。首先,我将视图修改为如下所示:

<div class="form-group col-lg-4">
            {!! Form::label('file1', 'Select file 1', ['class' => 'control-label']) !!}
            {!! Form::file('file1', ['id'=>'file1']) !!}
        </div>
        <div class="form-group col-lg-4">
            {!! Form::label('file2', 'Select file 2', ['class' => 'control-label']) !!}
            {!! Form::file('file2', ['id'=>'file2']) !!}
        </div>
        <div class="form-group col-lg-4">
            {!! Form::label('file3', 'Select file 3', ['class' => 'control-label']) !!}
            {!! Form::file('file3', ['id'=>'file3']) !!}
        </div>

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
                );
            }

        }