laravel Laravel集体表单,如何上传多张图片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46162609/
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 16:38:27  来源:igfitidea点击:

Laravel collective form, how to upload multiple images?

phpmysqllaravellaravelcollective

提问by Mark Walet

I wan't to change my form to enable more than one image to be uploaded at a time, this is what I currently have,

我不想更改我的表单以允许一次上传多个图像,这就是我目前拥有的,

part of my form:

我的表格的一部分:

{!! Form::open(['action' => 'PostsController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}

{{Form::file('stock_image')}}

my controller:

我的控制器:

if($request->hasFile('stock_image')){
        $filenameWithExt = $request->file('stock_image')->getClientOriginalName();
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        $extension = $request->file('stock_image')->getClientOriginalExtension();
        $fileNameToStore= $filename.'_'.time().'.'.$extension;
        $path = $request->file('stock_image')->storeAs('public/images', $fileNameToStore);
    } else {
        $fileNameToStore = 'noimage.jpg';
    }

thanks for your help!

感谢您的帮助!

回答by Mark Walet

For the file helper you should use unescaped rendering:

对于文件助手,您应该使用未转义的渲染:

{!! Form::file('stock_image') !!}

{!! Form::file('stock_image') !!}

If you want to upload multiple files you just have to use multiple input fields.

如果您想上传多个文件,您只需使用多个输入字段。

{!! Form::file('stock_image_1') !!}
{!! Form::file('stock_image_2') !!}
{!! Form::file('stock_image_3') !!}

If you want them in an array you can use the following syntax:

如果您希望它们在数组中,您可以使用以下语法:

{!! Form::file('stock_image[]') !!}
{!! Form::file('stock_image[]') !!}
{!! Form::file('stock_image[]') !!}

If you want to process the last one you can do the following:

如果要处理最后一个,可以执行以下操作:

An other tip: You can use the 'files' => trueoption for the Form::open()method instead of manually setting the enctype. It's a helper option.

另一个提示:您可以使用'files' => trueForm::open()方法的选项,而不是手动设置 enctype。这是一个辅助选项。