Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Call to a member function getClientOriginalExtension() on null in a Laravel upload

ErrorError: Call to a member function getClientOriginalExtension() on null

Short 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.

  • Applies to Laravel 12, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Dump what the request received for files, and what PHP reports for the field.

    dd($request->allFiles(), $_FILES);
  2. 2Match what you see, then jump to its fix

    1. 1$_FILES is an empty arrayThe form has no enctype="multipart/form-data"Fix
    2. 2$_FILES has a key, but $request->file() with your name is nullThe input name and the name in file() are differentFix
    3. 3$_FILES holds the field with error 1, or both $_FILES and $_POST are emptyThe file is larger than the PHP limitsFix

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 multipart

2Fix

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() output

Confirmed by php.net

Did this fix it?

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 again

Confirmed by laravel.com

Did this fix it?

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 = 25M

3Verify

Upload the file again and read the error code.

php -r "echo ini_get('upload_max_filesize'), PHP_EOL;"

Confirmed by php.net

Did this fix it?

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

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.