2017-08-19 50 views
0

我不知道我在哪里做错了没有上传的文件和名称没有存储在数据库中。图片上传不起作用laravel 5.4没有得到任何错误

这里是我的控制器

 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等,为确定我有选择正确的图像和扩展

回答

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> 
0
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”添加到您的表单属性中。

0

图片上传不能全面工作,问题是浏览器只是没有给服务器上传的完整文件路径。

要验证您可以输入类型的值做一个警报,明白我的意思

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

去到How to resolve the C:\fakepath?为全面解决问题的办法

读“答案chakroun yesser'