Laravel文件上传验证 [英] Laravel File Upload Validation

查看:67
本文介绍了Laravel文件上传验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手.我有一个带有文件上传功能的表单.如何验证他们的文件?我将只允许使用Microsoft Word文件.这是我的验证码.

I'm new to Laravel. I have a form with a File upload function on it. How can I validate their file? I will only allowed Microsoft Word files. Here's my validation code.

我只想检查他们是否插入了ms字文件,否则将不被处理.

I just want check if they insert a ms word file and if not it will not be processed.

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

推荐答案

要验证Laravel中输入的文件的mime类型,可以使用mimes规则.请记住将检测到的MIME类型与您提供的文件的实际MIME相匹配.在不同的服务器上可能会有所不同.

To validate mime type of a file input in Laravel you can use the mimes rule. Remember to match the mime type detected with the actual mime of file you provide. It may vary on different servers.

例如,您要启用表单中的添加和Word文档:

For example, you want to enable adding and word document in you form:

1)添加以下mime类型:

1) in config/mimes.php add the below mime types:

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

2)在您的验证$rules中添加以下元素:

2) in your validation $rules add the following elements:

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

这篇关于Laravel文件上传验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆