php Laravel:$request->hasFile() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41697980/
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
Laravel : $request->hasFile() is not working
提问by Maniruzzaman Akash
I have a form where i get the title
,description
and an image
. When i dd($requests->all());
, It returns the following which is correct.
我有一个表格,可以在其中获取title
,description
和image
. 当 i 时dd($requests->all());
,它返回以下正确的。
array:4 [
"projectTitle" => "asd"
"project_description" => "asd"
"project_image" => "15940723_1336567063030425_9215184436331587115_n.jpg"
"_token" => "eUj27iioySvIgut5Afu0ZHMgeVrO99a9e1o7Tw0w"
]
And i am storing the values as :
我将值存储为:
$project = new Portfolio;
$project->freelancer_id = Auth::user()->id;
$project->title = $request->get('projectTitle');
$project->description = $request->get('project_description');
if($request->hasFile('project_image')){
$project_image = $request->file('project_image');
$filename = time() . '.' . $project_image->getClientOriginalExtension();
Image::make($project_image)->resize(197, 137)->save( public_path('/uploads/portfolios/' . $filename ) );
$project->img = $filename;
}
$project->save();
But the img
DB table field gets null.
但是img
数据库表字段为空。
The if($request->hasFile('project_image'))
is not getting the field,
在if($request->hasFile('project_image'))
没有得到该领域,
Also i have form where the method is POST
and have enctype="multipart/form-data"
and a for file i have <input type="file" name="project_image" id="project_image">
.
另外我有形式,其中该方法是POST
与具有enctype="multipart/form-data"
和一个用于文件我有<input type="file" name="project_image" id="project_image">
。
What have i done wrong?
我做错了什么?
采纳答案by Paras
The error is not in your backend code. Looks like it's an error in your frontend code.
错误不在您的后端代码中。看起来这是您的前端代码中的错误。
As you can see the output of dd($request->all()) returned
正如你所看到的 dd($request->all()) 的输出返回
array:4 [
"projectTitle" => "asd"
"project_description" => "asd"
"project_image" => "15940723_1336567063030425_9215184436331587115_n.jpg"
"_token" => "eUj27iioySvIgut5Afu0ZHMgeVrO99a9e1o7Tw0w"
]
Your project_image
is just a string whereas it should have been an UploadedFile object/instance like so:
你project_image
只是一个字符串,而它应该是一个 UploadedFile 对象/实例,如下所示:
array:4 [
"projectTitle" => "asd"
"project_description" => "asd"
"project_file" => UploadedFile {#30
-test: false
-originalName: "15940723_1336567063030425_9215184436331587115_n.jpg"
-mimeType: "image/jpeg"
-size: 5126
-error: 0
}
"_token" => "eUj27iioySvIgut5Afu0ZHMgeVrO99a9e1o7Tw0w"
]
回答by Maniruzzaman Akash
If everything's gonna correct in your code, then you might be missing the enctype, I had also missed that at startup,
如果你的代码中的一切都会正确,那么你可能错过了enctype,我在启动时也错过了,
<form action="{{ route('store') }}" method="POST" enctype="multipart/form-data">
Or if you use Form Helper, that enctype easily included with file=truefield.
或者,如果您使用 Form Helper,那么该 enctype 很容易包含在file=true字段中。
{!! Form::open(['route' => ['store'], 'file' => true]) !!}
回答by Mahesh Yadav
You need to check php directive upload_max_filesizein php.ini.
您需要检查php.ini 中的php 指令upload_max_filesize。
Generally its size is 2M defined as default. If you upload file greater than this size
通常它的大小是 2M 定义为默认值。如果您上传的文件大于此大小
Laravel function
Laravel 函数
$request->hasFile()
$request->hasFile()
will return false
会返回假
Hope it helps!!
希望能帮助到你!!
回答by Mayank Pandeyz
Be sure you have an 'files' => true
option in your form definition like below:
确保您'files' => true
的表单定义中有一个选项,如下所示:
{!! Form::open(['route' => ['uploadimage'], 'files' => true]) !!}
This option causes that your form will be render with enctype="multipart/form-data"
option in HTML form tag, which is mandatory to upload a file using form
此选项会导致您的表单使用enctype="multipart/form-data"
HTML 表单标签中的选项呈现,这是使用上传文件所必需的form
Or, if you are using html form, then make sure you have enctype="multipart/form-data"
like:
或者,如果您使用的是 html 表单,请确保您有enctype="multipart/form-data"
:
<form action="{{ route('store') }}" method="POST" enctype="multipart/form-data">
回答by Muhammad Ali Rashed
I am using Laravel 5.5 and this worked for me (Found it on Laravel Docs https://laravel.com/docs/5.5/requests#files)
我正在使用 Laravel 5.5,这对我有用(在 Laravel Docs https://laravel.com/docs/5.5/requests#files上找到)
I replaced
我换了
if($request->hasFile('project_image')){...}
with
和
if ($request->file('project_image')!=null){...}
回答by Stephen Hendricks
Is the file getting stored on the server? Because you mention that the project_image
field is null, but you are never saving anything to project_image
, but you do try to save to the img
field with $project->img = $filename;
文件是否存储在服务器上?因为您提到该project_image
字段为空,但您从未将任何内容保存到project_image
,但是您确实尝试img
使用$project->img = $filename;