IIS returns 413 when a PHP form posts a large body
HTTP Error 413 Request Entity Too LargeShort 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.
Fix it fast
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\W3SVC12Match what you see, then jump to its fix
- 1
Substatus 413.1 in the IIS logmaxAllowedContentLength in request filtering is too smallFix - 2
The request reaches PHP, but $_POST and $_FILES are emptyPHP's own limits are lower than the new IIS limitFix - 3
The failure only appears with a module or extension that inspects the bodyuploadReadAheadSize is too small for the module that reads the bodyFix
- 1
What does the error look like?
HTTP Error 413 Request Entity Too LargeIIS 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/requestFiltering2Fix
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 logConfirmed by learn.microsoft.com
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 = 32M3Verify
phpinfo() for the web request shows the new values.
<?php phpinfo();Confirmed by php.net
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/serverRuntime2Fix
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:apphost3Verify
The upload completes with the module still active.
// repeat the uploadConfirmed by learn.microsoft.com
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 fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix