[英]Laravel : $request->hasFile() is not working
I have a form where i get the title
, description
and an image
.我有一个表格,我可以在其中获得
title
、 description
和image
。 When i dd($requests->all());
当我
dd($requests->all());
, It returns the following which is correct. , 它返回以下正确的。
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
DB 表字段为空。
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"
和一个 for 文件,我有<input type="file" name="project_image" id="project_image">
。
What have i done wrong?我做错了什么?
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=true field.或者,如果您使用 Form Helper,那么该 enctype 很容易包含在file=true字段中。
{!! Form::open(['route' => ['store'], 'file' => true]) !!}
You need to check php directive upload_max_filesize in php.ini.您需要检查 php.ini 中的 php 指令upload_max_filesize 。
Generally its size is 2M defined as default.通常它的大小是 2M 定义为默认值。 If you upload file greater than this size
如果您上传的文件大于此大小
Laravel function Laravel 函数
$request->hasFile()
$request->hasFile()
will return false会返回假
Hope it helps!!希望能帮助到你!!
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
此选项导致您的表单将在 HTML 表单标签中使用
enctype="multipart/form-data"
选项呈现,这是使用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">
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"
]
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){...}
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
,但您确实尝试使用$project->img = $filename;
保存到img
字段$project->img = $filename;
I had this problem but for me, problem is that I had used Form::model
instead of Form::open
.我有这个问题,但对我来说,问题是我使用了
Form::model
而不是Form::open
。 It should be:它应该是:
{!! Form::open(['url'=>'user/profile/' . $user->id, 'files'=>true) !!}
请使用 method="POST" 它对我有用
<form action="{{ route('store') }}" method="POST" enctype="multipart/form-data">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.