2017-10-12 139 views
0

我正嘗試上載一個圖像/ pdf到我的文件夾,並使用laravel 5.2在數據庫中保存名稱。即使我填寫所有字段,每按一次按鈕,我仍然會得到服務器端驗證錯誤。Laravel 5.2文件上傳不起作用

下面是我的看法(現在只給文件輸入字段。另外我在等形式的多個字段)

{!! Form::open(array('url' => 'college/add_infrastructure', 'role' => 
'form', 'method' => 'post', 'class' => 'clsForm', 'onsubmit'=>'return 
validateFormAddClient();', 'files'=> true)) !!} 

<div class="form-group"> 
    <label for="exampleInputEmail1">Upload Attachments</label> 
    <input type="file" name="attachments" id="attachments"> 
    <span id="error_attachments" style="color:#e03b3b;"> </span> 
</div> 

{!! Form::close() !!} 

控制器

public function add_infrastructure() { 


    $rules = array(
       'infrastructure_type_id' => 'required', 
       'type_of_waste' => 'required|regex:/^[(a-zA-Z\s)]+$/u', 
       'waste_quantity' => 'required|numeric', 
       'type_of_residues'=> 'required', 
       'residues_quantity' => 'required', 
       'utility_value' => 'required', 
       'attachments' => 'required|mimes:jpeg,jpg,png|max:10000' 
    ); 

    $validation = Validator::make(Input::all(), $rules); 

    if ($validation->fails()) 
    { 
     return Redirect::to('/college/infrastructure')->withErrors($validation); 
    } 

    else{ 
     $files = Input::file('attachments'); 
      $rules = array('files' => 'required'); 
      $validator = Validator::make(array('files'=> $files), $rules); 
       if($validator->passes()){ 
       $destinationPath = 'uploads/infrastructure'; 
       $filename = $files->getClientOriginalName(); 
       $path = $destinationPath.'/'.$filename; 

       $upload_success = $files->move($destinationPath, $filename); 
       $maildata['attachments']=$filename; 
       } 

      $user = Auth::user()->id; 
      $maildata['college_id'] = College::select('id')->where('user_id',$user)->first()->id; 
      $maildata['registration_id'] = Registration::select('id')->where('college_id',$maildata['college_id'])->first()->id; 

      $save = $this->save_data($maildata, 'ClgInfrastructure'); 

      $result = ClgInfrastructure::where('college_id',$maildata['college_id'])->where('registration_id',$maildata['registration_id'])->orderBy('id', 'desc')->get()->toArray(); 




      return redirect($current_url)->with('successmsg','Added Information Successfully.'); 
     } 


} 

結果總是驗證錯誤:'required'。 在此先感謝。

回答

0

您可以在您的表單中添加enctype="multipart/form-data",然後再試一次