简体   繁体   English

Laravel 文件上传验证

[英]Laravel File Upload Validation

I'm new to Laravel.我是 Laravel 的新手。 I have a form with a File upload function on it.我有一个带有文件上传 function 的表单。 How can I validate their file?我如何验证他们的文件? I will only allowed Microsoft Word files.我只允许 Microsoft Word 文件。 Here's my validation code.这是我的验证码。

I just want check if they insert a ms word file and if not it will not be processed.我只想检查他们是否插入了一个 ms word 文件,如果没有,它将不会被处理。

public function store()
{
  // Validate
    $rules = array(
        'pda'                         => 'required|unique:forms',
        'controlnum'                  => 'required|unique:forms',
        'date'                        => 'required',
        'churchname'                  => 'required',
        'title'                       => 'required',
        'pastorname'                  => 'required',
        'contactnum'                  => 'required',
        'address'                     => 'required',
        'state'                       => 'required',
        'region'                      => 'required',
        'area'                        => 'required',
        'city'                        => 'required',
        'zipcode'                     => 'required|numeric|max:9999',
        'tgjteachertraining'          => 'required',
        'localcontact'                => 'required',
        'tgjdatestart'                => 'required',
        'tgjdateend'                  => 'required',
        'tgjcourse'                   => 'required|numeric',
        'childrengraduated'           => 'required|numeric|max:450',
        'childrenacceptjesus'         => 'required|numeric',
        'howmanycomitted'             => 'required|numeric',
        'recievedbibles'              => 'required|numeric',
        'descgradevent'               => 'required',
        'whatwillyoudo'               => 'required',
        'pastortest'                  => 'required',
        'teachertest'                 => 'required',
        'childrentest'                => 'required',
        'file'                        => 'required|max:10000',
    );

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

    // process the form
    if ($validator->fails()) {
        return Redirect::to('forms/create')->withErrors($validator);
    } else {
        // store
        $forms = new Forms;
        $forms->pda                         = Input::get('pda');
        $forms->controlnum              = Input::get('controlnum');
        $forms->date                = Input::get('date');
        $forms->churchname                  = ucwords(Input::get('churchname'));
        $forms->title                       = ucwords(Input::get('title'));
        $forms->pastorname                  = ucwords(Input::get('pastorname'));
        $forms->address                     = Input::get('address');
        $forms->contactnum                  = Input::get('contactnum');
        $forms->state                       = Input::get('state2');
        $forms->region                      = Input::get('region2');
        $forms->area                        = Input::get('area2');
        $forms->citytown                    = Input::get('city2');
        $forms->zipcode                     = Input::get('zipcode');
        $forms->tgjteachertraining          = Input::get('tgjteachertraining');
        $forms->localcontact            = ucwords(Input::get('localcontact'));
        $forms->tgjdatestart            = Input::get('tgjdatestart');
        $forms->tgjdateend              = Input::get('tgjdateend');
        $forms->tgjcourse           = Input::get('tgjcourse');
        $forms->childrengraduated           = Input::get('childrengraduated');
        $forms->childrenacceptjesus     = Input::get('childrenacceptjesus');
        $forms->howmanycomitted         = Input::get('howmanycomitted');
        $forms->recievedbibles          = Input::get('recievedbibles');
        $forms->descgradevent           = Input::get('descgradevent');
        $forms->whatwillyoudo           = Input::get('whatwillyoudo');
        $forms->pastortest          = Input::get('pastortest');
        $forms->teachertest             = Input::get('teachertest');
        $forms->childrentest            = Input::get('childrentest');
        $file                   = Input::file('file');
        $filename                   = $file->getClientOriginalName(); 
        $destinationPath                = 'uploads/'.Input::get('pda');
        $uploadSuccess              = Input::file('file')->move($destinationPath, $filename);
        $forms->docurl              = 'uploads/'.Input::get('pda').'/'.$filename;
        if( $uploadSuccess ) {
        $forms->save();
        //Session::flash('message', 'Successfully submitted form!');
        return Redirect::to('forms/create'); 
        Session::flash('message', 'Successfully submitted form!');

        } 
        else {
        return Response::json('error', 400);
        }
    }
}

To validate mime type of a file input in Laravel you can use the mimes rule. 为了验证在Laravel你可以使用一个文件输入的MIME类型mimes规则。 Remember to match the mime type detected with the actual mime of file you provide. 请记住将检测到的mime类型与您提供的文件的实际mime相匹配。 It may vary on different servers. 它可能因服务器而异。

For example, you want to enable adding and word document in you form: 例如,您要在表单中启用添加和Word文档:

1) in config/mimes.php add the below mime types: 1)在config/mimes.php添加以下mime类型:

    'doc'  => array('application/msword', 'application/vnd.ms-office'),
    'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'), 

2) in your validation $rules add the following elements: 2)在您的验证$rules添加以下元素:

    'file' => 'required|max:10000|mimes:doc,docx' //a required, max 10000kb, doc or docx file

Try this? 试试这个?

'file' => 'required|max:10000|mimes:application/vnd.openxmlformats-officedocument.wordprocessingml.document'

You may want to set some custom message for the response though :) 你可能想为响应设置一些自定义消息:)

As of Laravel 9.22 you can write the validation rules a lot shorter and more readable like:Laravel 9.22 开始,您可以编写更短且更具可读性的验证规则,例如:

'file' => ['required', File::types(['doc', 'docx'])->smallerThan(10000)]

You can find the available methods in this pr: https://github.com/laravel/framework/pull/43271您可以在此 pr 中找到可用的方法: https://github.com/laravel/framework/pull/43271

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM