未在 laravel 8 中上传的图片未获取 hasFile
Image not uploading in laravel 8 didn't take hasFile
我正在尝试上传图像并将其保存到文件夹和数据库中。但是,当我提交表单时,会获取包括图像文件名在内的所有数据,但不会保存图像。在数据库中另存为 null 它做到了;如果条件 returns 为假,当我在图像内部回显时,它没有将图像作为文件获取,为什么?
这是代码
Blade
<form class="pt-3" action="{{url('registration_form')}}" method="post">
@csrf
<div class="form-group">
<input type="file" id="img" name="image" accept="image/*">
</div>
<div class="form-group">
<input type="text" name="name" class="form-control form-control-lg" placeholder="Enter your name" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control form-control-lg" placeholder="Email" required>
</div>
<div class="mt-3">
<input class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn" type="submit" value="SIGN UP">
</div>
<div class="text-center mt-4 font-weight-light">
Already have an account? <a href="{{url('login')}}" class="text-primary">Login</a>
</div>
</form>
控制器
public function registration_form(Request $request)
{
$user = new User;
if ($request->hasFile('image')) {
$image_tmp = $request->file('image');
if ($image_tmp->isValid()) {
$image_name = $image_tmp->getClientOriginalName(); //get the image name
$extension = $image_tmp->getClientOriginalExtension(); //get extention of the image
$imageName = $image_name . '-' . rand(111, 99999) . '.' . $extension;
$large_image_path = 'assets/images/profile' . $imageName;
Image::make($image_tmp)->save($large_image_path);
$user->image = $imageName;
}
}
$user->save();
}
回显$request数据时
Array
(
[_token] => 6xqdpIfqWBNMu0nifm91wM1lXpul0BRqoCTtEPmx
[image] => ERD.png
[name] => Amelia Alvarado
[email] => zekihap@mailinator.com
)
你错过了enctype="multipart/form-data"
enctype 属性指定 form-data 提交到服务器时应如何编码。
<form class="pt-3" action="{{url('registration_form')}}" method="post" enctype="multipart/form-data">
</form>
我正在尝试上传图像并将其保存到文件夹和数据库中。但是,当我提交表单时,会获取包括图像文件名在内的所有数据,但不会保存图像。在数据库中另存为 null 它做到了;如果条件 returns 为假,当我在图像内部回显时,它没有将图像作为文件获取,为什么? 这是代码
Blade
<form class="pt-3" action="{{url('registration_form')}}" method="post">
@csrf
<div class="form-group">
<input type="file" id="img" name="image" accept="image/*">
</div>
<div class="form-group">
<input type="text" name="name" class="form-control form-control-lg" placeholder="Enter your name" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control form-control-lg" placeholder="Email" required>
</div>
<div class="mt-3">
<input class="btn btn-block btn-primary btn-lg font-weight-medium auth-form-btn" type="submit" value="SIGN UP">
</div>
<div class="text-center mt-4 font-weight-light">
Already have an account? <a href="{{url('login')}}" class="text-primary">Login</a>
</div>
</form>
控制器
public function registration_form(Request $request)
{
$user = new User;
if ($request->hasFile('image')) {
$image_tmp = $request->file('image');
if ($image_tmp->isValid()) {
$image_name = $image_tmp->getClientOriginalName(); //get the image name
$extension = $image_tmp->getClientOriginalExtension(); //get extention of the image
$imageName = $image_name . '-' . rand(111, 99999) . '.' . $extension;
$large_image_path = 'assets/images/profile' . $imageName;
Image::make($image_tmp)->save($large_image_path);
$user->image = $imageName;
}
}
$user->save();
}
回显$request数据时
Array
(
[_token] => 6xqdpIfqWBNMu0nifm91wM1lXpul0BRqoCTtEPmx
[image] => ERD.png
[name] => Amelia Alvarado
[email] => zekihap@mailinator.com
)
你错过了enctype="multipart/form-data"
enctype 属性指定 form-data 提交到服务器时应如何编码。
<form class="pt-3" action="{{url('registration_form')}}" method="post" enctype="multipart/form-data">
</form>