mardi 12 septembre 2017

How to validate file types in Laravel

I am uploading a file on Laravel, and its validation doesn't seem to be working.

My controller code is as follows:

public function cv(Request $request) {
    if ($request->hasFile('cv')) {
        $file = $request->file('cv'); //i need this later to upload the file
        $rules= [
             'cv' => 'mimes:application/pdf'
        ];
        $x = $request->all();
        $validator=Validator::make($rules, $x);
        if ($validator->passes()){
            dd("Passes");
        }else{
            dd("fails");
        }
}}

My html code for my file is: <input type='file' name='cv'><br>

For some reason this code seems to fail, and the validator passes even if the file isn't a pdf. I only want the user to upload pdf files. doc files, docx files, amd text/plain files. Could someone help me out, I am unsure what I am doing incorrectly.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire