Laravel 文件上传验证

32

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

我只想检查他们是否插入了 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);
        }
    }
}
3个回答

63

在 Laravel 中验证文件输入的 Mime 类型,您可以使用 mimes 规则。请记住将检测到的 Mime 类型与提供的文件的实际 Mime 类型匹配。它可能因不同服务器而异。

例如,您想要在表单中启用添加和单词文档:

1)在 config/mimes.php 中添加以下 Mime 类型:

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

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

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

是的,创建它并以与其他配置文件相似的方式填充,作为键的数组。至于键 - 那就是我在答案中包含的内容 :) - Gadoma
嗨,谢谢!我按照你说的做了,并且遵循了这个链接 https://github.com/jasonlewis/laravel.com/blob/master/application/config/mimes.php但是我仍然收到错误提示: - Yassi
1
LogicException 无法猜测MIME类型,因为没有可用的猜测器(您启用了php_fileinfo扩展吗?) - Yassi
看起来你的服务器配置不完整,缺少一些必要的扩展程序以进行MIME类型检测?你使用的是什么HTTP服务器堆栈和操作系统?顺便说一句,如果我的回答解决了你最初的问题,请将其接受为官方答案,这样其他人在遇到类似问题时也可以受益。干杯! - Gadoma
嗨!我成功让它工作了。谢谢!我只是忘记在我的Wamp服务器上启用某些扩展程序了。谢谢!我给你加一个加号 :) - Yassi
@Gadoma 为什么不使用 mimetypes 而要进行配置? - francisco

3

试试这个?

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

你可能想为响应设置一些自定义消息 :)

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接