簡體   English   中英

Laravel 文件上傳驗證

[英]Laravel File Upload Validation

我是 Laravel 的新手。 我有一個帶有文件上傳 function 的表單。 我如何驗證他們的文件? 我只允許 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);
        }
    }
}

為了驗證在Laravel你可以使用一個文件輸入的MIME類型mimes規則。 請記住將檢測到的mime類型與您提供的文件的實際mime相匹配。 它可能因服務器而異。

例如,您要在表單中啟用添加和Word文檔:

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

試試這個?

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

你可能想為響應設置一些自定義消息:)

Laravel 9.22 開始,您可以編寫更短且更具可讀性的驗證規則,例如:

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

您可以在此 pr 中找到可用的方法: https://github.com/laravel/framework/pull/43271

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM