Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 validate csv file error on valid file type

I am trying to validate an uploaded csv file with

$validator = Validator::make(
        [
            'file'      => $file,
            'extension' => strtolower($file->getMimeType()),
        ],
        [
            'file'      => 'required|in:csv',
        ]
    );

The validator fails on giving a valid csv file with message The file must be a file of type: csv.

The validator passes if i remove in:csv.

Am i doing something wrong?

PS - $file is available and of standard upload file type object(Symfony\Component\HttpFoundation\File\UploadedFile)

like image 590
Swaraj Giri Avatar asked Mar 20 '15 05:03

Swaraj Giri


People also ask

How do I validate a csv file in laravel?

you can use required|mimes:csv,txt for validation csv file. Although is too late, validating the txt mime will allow ANY txt files, even any . sql file.


1 Answers

Try changing

$validator = Validator::make(
    [
        'file'      => $file,
        'extension' => strtolower($file->getMimeType()),
    ],
    [
        'file'      => 'required|in:csv',
    ]
);

To

$validator = Validator::make(
    [
        'file'      => $file,
        'extension' => strtolower($file->getClientOriginalExtension()),
    ],
    [
        'file'          => 'required',
        'extension'      => 'required|in:csv',
    ]
);
like image 57
Chris Townsend Avatar answered Oct 15 '22 06:10

Chris Townsend