Call to a member function getClientOriginalExtension() on null in a Laravel upload
Error: Call to a member function getClientOriginalExtension() on nullShort answer
The error means $request->file('name') returned null, so PHP or Laravel never received a file under that name. Check that the form uses enctype multipart/form-data, that the input name matches the one in file(), and that the file is under upload_max_filesize and post_max_size. Then guard the call with hasFile.
Fix it fast
1Find out which cause you have
Dump what the request received for files, and what PHP reports for the field.
dd($request->allFiles(), $_FILES);2Match what you see, then jump to its fix
- 1
$_FILES is an empty arrayThe form has no enctype="multipart/form-data"Fix - 2
$_FILES has a key, but $request->file() with your name is nullThe input name and the name in file() are differentFix - 3
$_FILES holds the field with error 1, or both $_FILES and $_POST are emptyThe file is larger than the PHP limitsFix
- 1
What does the error look like?
Error: Call to a member function getClientOriginalExtension() on null$request->file('photo') returns null although a file was chosen
Cause 1 of 3
The form has no enctype="multipart/form-data"
1Check
PHP only parses file parts when the form is sent as multipart. The PHP manual states the upload does not work without this attribute.
grep -rn "<form" resources/views/ | grep -v multipart2Fix
Add the enctype to the form and keep the method POST.
<form method="POST" action="/photos" enctype="multipart/form-data">
@csrf
<input type="file" name="photo">
</form>3Verify
Submit again; $_FILES now has an entry.
# submit the form and read the dd() outputConfirmed by php.net
Cause 2 of 3
The input name and the name in file() are different
1Check
The name attribute of the input is the key. Laravel reads the file by that name.
grep -rn "type=\"file\"" resources/views/2Fix
Use the same name in the view and in the controller, and use hasFile before reading it.
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->extension();
}3Verify
The upload now enters the branch.
# submit the form againConfirmed by laravel.com
Cause 3 of 3
The file is larger than the PHP limits
1Check
UPLOAD_ERR_INI_SIZE (1) means the file exceeds upload_max_filesize. A body above post_max_size leaves $_FILES and $_POST empty.
php -r "echo ini_get('upload_max_filesize'), ' ', ini_get('post_max_size'), PHP_EOL;"2Fix
Raise both values in the php.ini the web server uses, keep post_max_size above upload_max_filesize, and restart PHP-FPM or Apache. Check isValid() before using the file.
; php.ini
upload_max_filesize = 20M
post_max_size = 25M3Verify
Upload the file again and read the error code.
php -r "echo ini_get('upload_max_filesize'), PHP_EOL;"Confirmed by php.net
How do I stop it coming back?
- Always test the file with hasFile() and isValid() before calling methods on it.
- Add a validation rule for the file so a missing upload gives a form error instead of an exception.
- Remember that extension() guesses from the contents and may differ from the client's file name.
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
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack 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 fix