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