Laravel validator and excel files error

First tell that this is not the proper solution. But you can try this.

I've searched also for that and having so much trouble validating excel file and their mimes type is not working for that unfortunately.

if($request->hasFile('file'))
{
   $extension = File::extension($request->file->getClientOriginalName());
   if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
      //'Your file is a valid xls or csv file'
   }else {
      //'File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!');
   }
}

in namspace include must use File;

You can validate any file by this way, Thanks.


Here's how I did it in Laravel 6 by checking the file extension.

Create a new validation rule:

php artisan make:rule ExcelRule

Here is the ExcelRule, which checks the file extension:

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Http\UploadedFile;

class ExcelRule implements Rule
{
    private $file;

    public function __construct(UploadedFile $file)
    {
        $this->file = $file;
    }

    public function passes($attribute, $value)
    {
        $extension = strtolower($this->file->getClientOriginalExtension());

        return in_array($extension, ['csv', 'xls', 'xlsx']);
    }

    public function message()
    {
        return 'The excel file must be a file of type: csv, xls, xlsx.';
    }
}

As you can see, I'm checking for csv, xls, or xlsx here. You can add any additional extensions you desire.

Using it in a controller:

public function uploadExcelFile(Request $request)
{
    $request->validate([
        'excel_file' => ['required', new ExcelRule($request->file('excel_file'))],
    ]);

    $model->update([
        'excel_file' => $request->file('excel_file')->store('excel_files'),
    ]);

    return redirect()->route('my_route_name')->with('Excel file uploaded!');
}

Ok, my fault. I had tried another solution, found on this website and it worked. Thanks for help Odin. It was my first question on this website. I am gonna see if I can help someone now. I post code for solution for someone in need :).

$validator = Validator::make(
  [
      'file'      => $request->file,
      'extension' => strtolower($request->file->getClientOriginalExtension()),
  ],
  [
      'file'          => 'required',
      'extension'      => 'required|in:doc,csv,xlsx,xls,docx,ppt,odt,ods,odp',
  ]
);

Use "mimes" when you want to write an extentions (xlsx,doc,docx). In case when use mime-type like application/vnd.ms-excel you must use validation rule mimetype

More mime types: more mime-types

$validator=Validator::make($request->all(),[
 //use this
    'file'=>'required|max:50000|mimes:xlsx,doc,docx,ppt,pptx,ods,odt,odp'
 //or this
    'file'=>'required|max:50000|mimetypes:application/csv,application/excel,
        application/vnd.ms-excel, application/vnd.msexcel,
        text/csv, text/anytext, text/plain, text/x-c,
        text/comma-separated-values,
        inode/x-empty,
        application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]);