【问题标题】:Image upload not work laravel 5.4 doesn't get any error图片上传不起作用 laravel 5.4 没有任何错误
【发布时间】:2018-01-28 06:07:01
【问题描述】:

我不知道我在哪里做错了文件没有上传并且名称没有存储在数据库中。

这是我的控制器

      if(Input::hasFile('photo')) {
        $fotoName = 'peg' . $employee->id . '.' .
        $request->file('photo')->getClientOriginalExtension();
        $request->file('photo')->move(
        base_path() . '/public/images/employee/', $fotoName
        );
        $img = Image::make(base_path() . '/public/images/employee/' . $fotoName);
        $img->resize(150, null, function ($constraint) {
          $constraint->aspectRatio();
        });
        $img->save();
        $employee_fotos = Karyawan::find($employee->id);
        $employee_fotos->photo = $fotoName;
        $employee_fotos->save();
      }

观看次数

<form class="form-horizontal" role="form" action="{{ route("karyawan.store") }}" method="post" multiple>
        {{ csrf_field() }}
    <div class="row">
      <!-- left column -->
      <div class="col-md-3">
        <div class="text-center">
          <img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
          <h6>Upload a different photo...</h6>

          <input type="file" name="photo" class="form-control" />
        </div>
      </div>
</form>

我没有收到任何错误,如果我验证它总是得到请添加图像或确保文件扩展名 .jpg 等,确保我选择了正确的图像和扩展名

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    enctype="multipart/form-data" 添加到您的form 标签:

    <form class="form-horizontal" enctype="multipart/form-data" role="form" action="{{ route("karyawan.store") }}" method="post" multiple>
            {{ csrf_field() }}
        <div class="row">
          <!-- left column -->
          <div class="col-md-3">
            <div class="text-center">
              <img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
              <h6>Upload a different photo...</h6>
    
              <input type="file" name="photo" class="form-control" />
            </div>
          </div>
    </form>
    

    【讨论】:

      【解决方案2】:
      public function store(Request $request) 
      {
      
          //dd(request()->all());
      
          $this->validate(request(), [
              'file'              => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6000',
      
          ]);
      
      
      
          $imageName = time().'.'.$request->file->getClientOriginalExtension();
          $request->file->move(public_path('/images/employee/'), $imageName);
      

      }

      这应该对你有用,但它会存储带有 unix 时间戳名称的图像。如果您使用雇主 ID 来存储图像,那么下次您使用相同的员工帐户上传新图像时,您的存储文件夹中可能会有重复的图像。 还将 enctype="multipart/form-data" 添加到您的表单属性中。

      【讨论】:

        【解决方案3】:

        图片上传无法正常工作,问题是浏览器没有提供服务器上传的完整文件路径。

        为了验证您可以对输入类型的值进行警报以了解我的意思

        <input type="file" name="photo" onchange="alert(this.value)" class="form-control" />
        

        转到How to resolve the C:\fakepath? 以获取问题的完整解决方案

        阅读“chakroun yesser”的答案

        【讨论】:

          猜你喜欢
          • 2017-08-02
          • 2019-09-16
          • 2017-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多