laravel 5.4 图片上传未检测到文件

laravel 5.4 image upload no file detected

这是我的 code.It return 每次都没有文件。我有一个名称="file" 的输入字段随此表单一起发送。

请帮助我诊断我的代码,以便我能理解为什么会发生这种情况。谢谢。

<div class="container">
<div class="row section-register">
    <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">New Auction</div>
            <div class="panel-body">
                <form class="form-horizontal" role="form" method="POST" action="{{ url('/addproduct') }}">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label for="itemname" class="col-md-2 control-label">Item name</label>

                        <div class="col-md-10">
                            <input id="itemname" type="text" class="form-control" name="itemname" value="" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="itemdesc" class="col-md-2 control-label">Decsription</label>

                        <div class="col-md-10">
                            <textarea id="itemdesc" class="form-control" name="itemdesc" value="" required></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="category" class="col-md-2 control-label">Category</label>

                        <div class="col-md-10">
                            <select id="category" class="form-control" name="category" required>
                                <option value="Games">Games</option>
                                <option value="Gadgets">Gadgets</option>
                                <option value="Books">Books</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="bid" class="col-md-2 control-label">Starting bid</label>

                        <div class="col-md-10">
                            <input id="bid" type="number" class="form-control" name="bid" value="" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="increment" class="col-md-2 control-label">Incremental value</label>

                        <div class="col-md-10">
                            <select id="increment" class="form-control" name="increment" required>
                                <option value="10">10</option>
                                <option value="20">20</option>
                                <option value="50">50</option>
                                <option value="100">100</option>
                                <option value="200">200</option>
                                <option value="300">300</option>
                                <option value="500">500</option>
                                <option value="1000">1000</option>
                            </select>
                        </div>
                    </div>
                    <br>
                    <p class="text-center warning-img">Images uploaded to SpinkyDink are resized. For the best quality images on your auction, please use land-scape images instead of portrait images.
                    <br>(Turn your phone sideways and snap a pic)</p>
                    <br>
                    <div class="form-group">
                        <label for="file" class="col-md-2 control-label">Main Image</label>

                        <div class="col-md-10 text-center">
                            <input id="file" type="file" class="form-control" name="file" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-2">
                            <input id="file2" type="file" class="form-control" name="file2">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-2">
                            <input id="file3" type="file" class="form-control" name="file3">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-2">
                            <input id="file4" type="file" class="form-control" name="file4">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-12 text-center">
                            <button type="submit" class="btn btn-success">
                                Submit Auction
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

路线:

Route::post('/addproduct', 'NewProductController@addproduct');

控制器

public function addproduct(Request $request)
{
    if ($request->hasFile('file')) {
        echo "file found";
    }else{
        echo "no file";
    }
}

每次我点击这个控制器时,回显 "no file" 行都会 return。

向表单添加 enctype 属性:

<form class="form-horizontal" role="form" method="POST" action="{{ url('/addproduct') }}" enctype="multipart/form-data">

默认情况下,数据会被编码,但如果您想使用表单上传文件,您必须禁用编码,这就是 multipart/form-data 所做的。