Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get size of POST-request in PHP

Tags:

Is there any way to get size of POST-request body in PHP?

like image 684
Andrey M. Avatar asked Sep 01 '09 09:09

Andrey M.


People also ask

How to get POST request 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.

What is the size limit of a POST request?

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.

How to get POST data in php api?

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.


1 Answers

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

like image 191
scoffey Avatar answered Sep 20 '22 14:09

scoffey