繁体   English   中英

图像上传错误laravel 5.6

[英]Image uploading error laravel 5.6

我对在Laravel 5.6中上传图片感到困惑。 文字输入正常,但图片未上传。

为了测试路径和输入字段,我放置了一个演示回显来检索文本列数据,并且显示效果非常好。 但是图片未上传...

但是我在上一个项目中使用了相同的代码来上传图像,这很好。.这次不起作用。

控制器-

<?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 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>

您在表单上缺少enctype属性:

<form role="form" action="{{url('/children/add')}}" method="post" enctype="multipart/form-data">

multipart/form-data允许整个文件包含在请求数据中。 对于编写表格,这是您真正需要知道的全部。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM