2014-05-13 11 views
19

Jestem nowy w firmie Laravel. Mam formularz z funkcją przesyłania plików. Jak mogę sprawdzić ich plik? Dopuszczam tylko pliki Microsoft Word. Oto mój kod weryfikacyjny.Laravel File Upload Validation

Chcę tylko sprawdzić, czy wstawiają plik MS Word, a jeśli nie, to nie zostaną przetworzone.

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

Odpowiedz

35

Aby sprawdzić typ MIME pliku wejściowego w laravel można wykorzystać regułę mimes. Pamiętaj, aby dopasować typ MIME wykrytego do rzeczywistego pliku mime, który podasz. Może się różnić na różnych serwerach.

Na przykład, chcesz, aby umożliwić dodawanie i dokument słowo ty tworzą:

1) w config/mimes.php dodać poniższe typy MIME:

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

2) w walidacji $rules dodać następujące elementy :

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

tak, utwórz i wypełnij podobnie jak pozostałe pliki konfiguracyjne, jako tablicę kluczy. Co do kluczy - to jest to, co zawarłem w mojej odpowiedzi :) – Gadoma

+0

Cześć Dzięki! Zrobiłem to, co powiedziałeś i postępuj zgodnie z tym https://github.com/jasonlewis/laravel.com/blob/master/application/config/mimes.php Ale nadal mam błąd: – Yassi

+1

LogicException Nie można odgadnąć typu MIME ponieważ nie ma dostępnych narzędzi do odgadywania (Czy włączono rozszerzenie php_fileinfo?) – Yassi

2

Spróbuj tego?

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

Możesz ustawić jakiś niestandardowy komunikat na odpowiedź choć :)

Powiązane problemy