[英]Image uploading error laravel 5.6
I am stuck with image uploading in Laravel 5.6. 我对在Laravel 5.6中上传图片感到困惑。 text input is fine but image not uploading. 文字输入正常,但图片未上传。
For testing route and input field, I put a demo echo for retrieve text column data and it show perfectly. 为了测试路径和输入字段,我放置了一个演示回显来检索文本列数据,并且显示效果非常好。 But the image is not uploading... 但是图片未上传...
But I used same code in my previous project to image uploading and that was good..this time is not working. 但是我在上一个项目中使用了相同的代码来上传图像,这很好。.这次不起作用。
controller- 控制器-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
class UserController extends Controller
{
public function insertChildren(Request $request){
echo $request->input('firstName');
//this echo doing well
if ($request->hasFile('image')) {
$imageExtention=$request->file('image')->getClientOriginalExtension();
$randomString=substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 10)), 0, 10);
$modifiedImageName=$randomString.'.'.$imageExtention;
$productImage->move(public_path('img'),$modifiedImageName);
}
}
Form 形成
<form role="form" action="{{url('/children/add')}}" method="post">
@csrf
<div class="form-group ml-4 mr-4">
<div class="row">
<div class="col-6">
<label for="name">First Name</label>
<input value="{{ old('firstName') }}" name="firstName" type="text" class="form-control" id="first" placeholder="First Name">
</div>
<div class="col-6">
<label for="name">Last Name</label>
<input value="{{ old('lastName') }}" name="lastName" type="text" class="form-control" id="last" placeholder="Last Name">
</div>
</div>
</div>
<div class="form-group ml-4 mr-4">
<label for="gender">gender</label>
<br>
<input value="{{ old('gender') }}" type="text" name="gender" class="form-control" placeholder="Boy or Girl or Other">
</div>
<div class="form-group ml-4 mr-4">
<label for="Birthday">Birthday</label>
<input name="birthday" type="date" class="form-control" id="birthday">
</div>
<div class="form-group ml-4 mr-4">
<label for="image">Profile Image</label>
<input type="file" name="image" class="form-control" id="image">
</div>
<button type="submit" style="background-color:#2DAE60;" class="btn">
<i class="fa fa-plus"></i> ADD </button>
</form>
You're missing enctype
attribute on your form: 您在表单上缺少enctype
属性:
<form role="form" action="{{url('/children/add')}}" method="post" enctype="multipart/form-data">
multipart/form-data
allows entire files to be included in the request data. multipart/form-data
允许整个文件包含在请求数据中。 For writing forms, that's all you need to know really. 对于编写表格,这是您真正需要知道的全部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.