我尝试使用Laravel控制器上传图片
public function store(Request $request)
{
$new=new Verify;
$new->user_id=Auth::id();
$new->message=$request->message;
if($request->hasfile('image'))
{
$file=$request->file('image');
$extension=$file->getClientOriginalExtension();
$filename=time() ."." . $extension;
$file->move('uploads/Verify',$filename);
$new->image=$filename;
}
$new->save();
return redirect()->back();
我的迁移表是这样的
public function up()
{
Schema::create('verifies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->nullable();
$table->string('message')->nullable();
$table->string('image')->nullable();
$table->timestamps();
});
}
诸如用户ID和信息等字段被填入,但图片字段仍然为空,代码的html是这样的
<table class="table">
<tr>
<th>Your message</th>
</tr>
<tr>
<td>
<form action="{{route('Verifying')}}" method="POST">
@csrf
<textarea name="message" id="message" cols="5" rows="5"></textarea>
</td>
</tr>
<tr>
<th>Your Cnic</th>
</tr>
<tr>
<td>
<input type="file" name="image" id="image">
</td>
</tr>
<tr>
<td>
<input type="submit">
</td>
</tr>
</form>
</table>
谁能告诉我是什么问题? 为什么图片不能上传...
表格发送与 enctype="multipart/form-data"
那就好办了
<form action="{{route('Verifying')}}" method="POST" enctype="multipart/form-data">
那就可以了为什么我们要加上 enctype
enctype属性指定了向服务器提交表单数据时应如何编码。