在 Laravel 5.0 上上传图片为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26216702/
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
Uploading Image null on Laravel 5.0
提问by Set Kyar Wa Lar
I had created a form like the following in my view
在我看来,我创建了一个如下所示的表单
{!! Form::open(array('url' => 'user/posts', 'files'=> true)) !!}
<div class="form-group">
{!! Form::text('title',Input::old('title'),['class'=>'form-control', 'placeholder' => 'What is your title?']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Your Image') !!}
{!! Form::file('image') !!}
<p class="help-block">Hey! Please don't upload over 15MB images!</p>
</div>
<div class="form-group">
{!! Form::label('caption', 'Don\'t miss to caption!') !!}
{!! Form::text('caption',Input::old('caption'),['class'=>'form-control', 'placeholder' => 'Your caption']) !!}
</div>
{!! Form::submit('Post now!',['class'=>'btn btn-info']) !!}
{!!Form::close() !!}
And I try dd() for my image from my controller but it return nulll. The following one is my Controller
我从我的控制器尝试 dd() 为我的图像,但它返回 nulll。下面一张是我的Controller
public function store(PostFormRequest $request)
{
dd($request->input('image'));
$post = new Post;
}
The following one is my PostFormRequest
下面一张是我的 PostFormRequest
<?php namespace App\Http\Requests;
use Response;
use Illuminate\Foundation\Http\FormRequest;
class PostFormRequest extends FormRequest {
public function rules()
{
return [
'title' => 'required',
'image' => 'mimes:jpeg,bmp,png'
];
}
public function authorize()
{
$image = $this->image;
if(empty($image))
return true;
else
return false;
}
public function forbiddenResponse()
{
return Response::make('Sorry!',403);
}
}
When I try dd($request->input('title'));
it's return string(4) "test"
what I was wrong?
当我尝试dd($request->input('title'));
它时返回string(4) "test"
我错了什么?
回答by Marcin Nabia?ek
回答by Andrey P.
Faced with the same problem. One more little thing: if you use native HTML form submit you need also set 'enctype="multipart/form-data"'. Otherwise $request->file('INPUT_NAME') returns string instead the file.
面临同样的问题。还有一件小事:如果您使用原生 HTML 表单提交,您还需要设置“enctype="multipart/form-data"”。否则 $request->file('INPUT_NAME') 返回字符串而不是文件。