abdulkader's avatar

Excel file validation in laravel dose not work

$this->validate($request,
[
   'file'=> 'required|mimes:xlsx'
]);

When i try to upload a .xlsx file, the validator fail.

0 likes
5 replies
MultCompiler's avatar

I tried validators provided by laravel but all doesnt seems to work.. That gave me an idea to try to validate using normal php and it works like a cham

$extensions = array("xls","xlsx","xlm","xla","xlc","xlt","xlw");

$result = array($request->file('import_file')->getClientOriginalExtension());

if(in_array($result[0],$extensions)){
     // Do something when Succeeded 
}else{
   // Do something when it fails
}
Barracuda's avatar

The issue with this solution is that an attacker could bypass your security by just changing the extension of the file. Mimes validation inspect the content of the file to make sure it is truly the desired type.

Hadayat's avatar

Simple solution is using mime

 $request->validate([
    'file'=> 'required|mimes:xlsx, csv, xls'
 ]);
2 likes

Please or to participate in this conversation.