2017-08-19 48 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'