I'm doing very secure and tight photo upload with multiple validations. All is set up. However I am stuck at limiting the incoming body size. I'm using a custom method to upload, images are usually between 0.5-5 MB, and I would like to force 5 MB as the limit. A custom method works as an encrypted JSON array passed with some parameters and a JSON field with image b64 string.
$size = (float)$_SERVER['CONTENT_LENGTH']/1024*1024;
if ($size > 5) {
die('file too big');
}
This code does not exactly do what I'm looking for, because it waits until the entire body is received which makes this code useless. Is there a way to read the CONTENT_LENGTH header before or during uploading the POST body to drop it if necessary?