Excel File validation

Submitted by decipher - 6 years ago

Hello there! How to do Excel file validation in laravel? In Laravel you can use validate the file upload with extension using After Hooks. Read more from https://laravel.com/docs/5.5/validation#after-validation-hook

$validator->after(function ($validator) use ($request){
        if($this->checkExcelFile($request->file('file')->getClientOriginalExtension()) == false) {
            //return validator with error by file input name
            $validator->errors()->add('file', 'The file must be a file of type: csv, xlsx, xls');
        }
    });

    function checkExcelFile($file_ext){
        $valid=array(
            'csv','xls','xlsx' // add your extensions here.
        );        
        return in_array($file_ext,$valid) ? true : false;
    }