【问题标题】:unable to uplaod an image in laravel 5.8无法在 laravel 5.8 中上传图片
【发布时间】:2020-10-03 02:49:45
【问题描述】:

我尝试使用 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>

谁能指导我出了什么问题?为什么图片没有上传...

【问题讨论】:

  • 您没有上传文件..您的表单未设置为处理多部分表单数据

标签: laravel eloquent laravel-5.8


【解决方案1】:

使用enctype="multipart/form-data" 发送表单然后它应该可以工作

<form action="{{route('Verifying')}}" method="POST" enctype="multipart/form-data">

然后它会工作 我们为什么要添加enctype enctype 属性指定表单数据在提交到服务器时应如何编码。

【讨论】:

    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 2019-09-05
    • 2020-03-21
    • 2018-04-17
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多