Laravel 5.2文件上传不起作用

时间:2022-10-18 17:24:12

I am trying to upload an image/pdf to my folder and save the name in database using laravel 5.2 . On every button click I used to get server side validation errors even though I fill all fields.

我正在尝试将image / pdf上传到我的文件夹,并使用laravel 5.2将名称保存在数据库中。在每个按钮单击时,我习惯得到服务器端验证错误,即使我填写所有字段。

The below is my view (now giving only file input field. Also I have some more fields in the form)

以下是我的观点(现在只给出文件输入字段。另外我在表单中还有一些字段)

{!! 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() !!}

Controller

调节器

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.'); 
        }


}

Result is always the validation error : 'required'. Thanks in advance.

结果总是验证错误:'required'。提前致谢。

1 个解决方案

#1


0  

Could you add enctype="multipart/form-data" in your form and then try again

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

#1


0  

Could you add enctype="multipart/form-data" in your form and then try again

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