Is there any way to get size of POST-request body in PHP?
The $_REQUEST variableThe PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods. Try out following example by putting the source code in test. php script. Here $_PHP_SELF variable contains the name of self script in which it is being called.
The default value of the HTTP and HTTPS connector maximum post size is 2MB. However you can adjust the value as per your requirement. The below command to set the connector to accept maximum 100,000 bytes. If the http request POST size exceeds the 100,000 bytes then connector return HTTP/1.1 400 Bad Request.
From the PHP manual entry on I/O streamsdocs: php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php. ini directives.
As simple as:
$size = (int) $_SERVER['CONTENT_LENGTH'];
Note that $_SERVER['CONTENT_LENGTH']
is only set when the HTTP request method is POST (not GET). This is the raw value of the Content-Length
header, as specified in RFC 7230.
In the case of file uploads, if you want to get the total size of uploaded files, you should iterate over the $_FILE
array to sum each $file['size']
. The exact total size might not match the raw Content-Length
value due to the encoding overhead of the POST data. (Also note you should check for upload errors using the $file['error']
code of each $_FILES
element, such as UPLOAD_ERR_PARTIAL
for partial uploads or UPLOAD_ERR_NO_FILE
for empty uploads. See file upload errors documentation in the PHP manual.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With