繁体   English   中英

Laravel 5:上传多个文件和其他输入

[英]Laravel 5: upload multiple files and other input

我在使用Laravel 5的表单时遇到麻烦。当我将enctype属性指定为“ multipart / form-data”时,我收到令牌不匹配错误。 如果将其删除,则表单始终无法通过我的控制器中指定的验证。

的HTML

<form class="lajax" action="{{ action('AlbumController@store') }}" method="POST">
                    <div class="form-group">
                        <label>Album Name</label>
                        <input type="text" name="name" class="form-control">                                                
                    </div>

                    <div class="form-group">
                        <label for="coverFile">Album Cover Image</label>
                        <input  name="cover" type="file" id="coverFile">
                        <p class="help-block">Example block-level help text here.</p>
                    </div>

                    <div class="form-group">
                        <label for="albumFiles">Album Images</label>
                        <input type="file" name="photos[]" multiple>
                    </div>

                    <button type="submit" class="btn btn-primary">Create Album</button>

                    {{ csrf_field() }}
                </form>

控制者

public function store(Request $request)
    {

        //request input verification rules
        $rules=[
            'name'=>'required',
            'cover'=>'required|image',
            'photos'=>'required|array',
            'photos.*'=>'image'
        ];

        //perform validation
        $this->validate($request,$rules);

       // blah blah
    }

具体来说,图像似乎有问题。

报告的错误:cover不是图像,photo.0不是图像,photo.1不是图像.....,依此类推。

请帮忙

更改:

<form class="lajax" action="{{ action('AlbumController@store') }}" method="POST">

至:

<form method="POST" action="{{ action('AlbumController@store') }}" accept-charset="UTF-8" enctype="multipart/form-data">

在控制器中,您可以像这样检查输入:

$request->hasFile('file_input_name');

还要检查Laravel Collective以创建表单: https ://laravelcollective.com/

我发现了错误! 它在我的php.ini文件中。 我将3M的post_max_size更改为1000M。 有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM