Laravel 5.2文件上传不起作用

时间:2017-10-12 08:14:54

标签: php file-upload laravel-5.2

我正在尝试将image / pdf上传到我的文件夹,并使用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'。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在表单中添加enctype="multipart/form-data",然后重试