Maaz Zindani

Loading the index…

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

IIS returns 413 when a PHP form posts a large body

ErrorHTTP Error 413 Request Entity Too Large

Short answer

IIS request filtering refuses any request body larger than maxAllowedContentLength, 30,000,000 bytes by default, before PHP runs. Raise that value in web.config. Then make sure post_max_size and upload_max_filesize in PHP allow the same size, or PHP will drop the upload silently.

  • Applies to IIS 10, PHP 8.3 via FastCGI
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the IIS log for the failed request and read the status and substatus columns. 404 or 413 with substatus 413.1 points to request filtering. If the log shows no 413.1, go to the PHP limits.

    %SystemDrive%\inetpub\logs\LogFiles\W3SVC1
  2. 2Match what you see, then jump to its fix

    1. 1Substatus 413.1 in the IIS logmaxAllowedContentLength in request filtering is too smallFix
    2. 2The request reaches PHP, but $_POST and $_FILES are emptyPHP's own limits are lower than the new IIS limitFix
    3. 3The failure only appears with a module or extension that inspects the bodyuploadReadAheadSize is too small for the module that reads the bodyFix

What does the error look like?

  • HTTP Error 413 Request Entity Too Large
  • IIS log shows substatus 413.1 (Content Length Too Large)
  • The upload works for small files and fails above about 28 MB

Cause 1 of 3

maxAllowedContentLength in request filtering is too small

1Check

Request filtering blocks a request whose content is longer than maxAllowedContentLength, in bytes. The default is 30000000, about 28.6 MB. The client may see a 404 while the log records 413.1.

%windir%\system32\inetsrv\appcmd.exe list config "Default Web Site" -section:system.webServer/security/requestFiltering

2Fix

Set a limit that fits your biggest legal upload, in bytes, in the site's web.config. The example allows 100 MB.

<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="104857600" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

3Verify

A file above the old limit now reaches PHP, and the log shows 200.

// repeat the upload and read the IIS log

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

PHP's own limits are lower than the new IIS limit

1Check

post_max_size defaults to 8M and upload_max_filesize to 2M. When the post data exceeds post_max_size, $_POST and $_FILES are empty.

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

2Fix

Raise both in php.ini, with post_max_size larger than upload_max_filesize, and memory_limit larger than post_max_size.

memory_limit = 256M
post_max_size = 64M
upload_max_filesize = 32M

3Verify

phpinfo() for the web request shows the new values.

<?php phpinfo();

Confirmed by php.net

Did this fix it?

Cause 3 of 3

uploadReadAheadSize is too small for the module that reads the body

1Check

uploadReadAheadSize is the number of bytes IIS reads into a buffer and passes to a module or ISAPI extension; the default is 49152. The docs warn that raising it increases memory use.

%windir%\system32\inetsrv\appcmd.exe list config -section:system.webServer/serverRuntime

2Fix

Raise it only to what the application needs and keep it as low as you can.

%windir%\system32\inetsrv\appcmd.exe set config "Default Web Site" -section:system.webServer/serverRuntime /uploadReadAheadSize:1048576 /commit:apphost

3Verify

The upload completes with the module still active.

// repeat the upload

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Set the limit in IIS and in PHP together, and keep them equal.
  • Show the real maximum on the upload form so people see it before they try.
  • Keep limits close to what the application needs; do not use the maximum value.

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

A PHP upload fails or $_FILES comes back empty

$_POST and $_FILES are both empty on a large upload

A PHP upload fails for one of four reasons: the temporary directory is not writable, the whole POST is bigger than post_max_size, one file is bigger than upload_max_filesize, or there are more files than max_file_uploads.

4 causes, each with a fix

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

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