A CSV or Excel upload fails Laravel's mimes validation although the extension is right
Validation fails on a .csv or .xlsx upload that opens fine in ExcelShort answer
Laravel's mimes rule and File::types() do not trust the file name. They read the file's contents, guess a MIME type, and compare it to the extensions you listed. A CSV saved by Excel, or a spreadsheet renamed to .csv, can be guessed as something else and fail. Print the guess, then validate with the extension rule plus a content rule.
Fix it fast
1Find out which cause you have
Print what Laravel actually sees for the rejected file: the extension it guessed from the contents and the extension the user gave it. They usually differ.
$file = $request->file('upload'); logger()->info('upload check', [ 'guessed_extension' => $file->extension(), 'client_name' => $file->getClientOriginalName(), ]);2Match what you see, then jump to its fix
- 1
The file opens fine in Excel, the name ends in .csv or .xlsx, yet validation says it is not that typeThe content guess does not match the extensions in the ruleFix - 2
A .xls or .ods file saved with a .csv or .xlsx name is rejected, or a file named .txt with valid content is acceptedThe file was renamed, so name and content disagreeFix
- 1
What does the error look like?
Validation fails on a .csv or .xlsx upload that opens fine in Excelmimes:csv or File::types(['csv']) rejects the file
Cause 1 of 2
The content guess does not match the extensions in the rule
1Check
mimes and File::types() read the file's contents and guess a MIME type, which can differ from what the browser sent. Compare the guessed extension from the first step with your list.
// in tinker or a test, for the failing file
$file->extension();2Fix
Look up the type that matches your real files in the Apache mime.types list the Laravel docs point to, then validate the content with mimetypes and the user-given name with extensions, as the docs recommend using them together.
$request->validate([
'upload' => [
'required',
'file',
'extensions:csv,xlsx',
'mimetypes:text/plain,text/csv', // add the types your real files report
],
]);3Verify
Upload one real .csv and one real .xlsx and confirm both pass.
php artisan test --filter=ImportUploadTestConfirmed by laravel.com
Cause 2 of 2
The file was renamed, so name and content disagree
1Check
The mimes rule does not check that the extension and the content agree. A file whose content is a different format fails a mimes list; a file with right content but another name can still pass it.
$file->extension(); // guessed from the contents
$file->getClientOriginalExtension(); // what the user typed2Fix
Tell the user to export again in the requested format. In the rule, keep the extensions rule next to the content rule so both the name and the contents have to match.
'upload' => ['required', 'extensions:csv', 'mimetypes:text/plain,text/csv'],3Verify
Rename a PDF to .csv and confirm it is now rejected.
php artisan test --filter=ImportUploadTestConfirmed by laravel.com
How do I stop it coming back?
- Keep one test per accepted format that uploads a real sample file, so a framework or PHP upgrade that changes the content guess is caught.
- Use extensions together with mimes or mimetypes, never the extension alone.
- Show the user the accepted formats next to the upload field so they export correctly the first time.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix