You can limit the maximum size of uploaded files in two ways:
- Include
<input type="hidden" name="MAX_FILE_SIZE" value="..."/> in the HTML form; - Configure
upload_max_filesize in php.ini.
If you try to upload a file, upload_max_filesize larger than upload_max_filesize PHP will display a warning before running the PHP script:
Warning: POST content length of 14259306 bytes exceeds the limit of 8388608 bytes in the "Unknown" field on line 0
$_FILES and $_POST['MAX_FILE_SIZE'] in this case will be empty. It seems that UPLOAD_ERR_INI_SIZE never works. Anyway, I never caught this in my code.
When the upload file is smaller than upload_max_filesize some elements of the $_FILES may contain UPLOAD_ERR_FORM_SIZE errors for files, UPLOAD_ERR_FORM_SIZE exceeds MAX_FILE_SIZE .
You can catch both situations with the following PHP script:
<?php $warning = error_get_last(); if ($warning !== null && stripos($warning['message'], 'POST Content-Length of') !== false) { error_clear_last(); echo 'Upload is bigger than '.ini_get('upload_max_filesize'); } else if ($_FILES['input_name']['error'] == UPLOAD_ERR_FORM_SIZE) { echo 'Upload is bigger than '.$_POST['MAX_FILE_SIZE']; }
Aplextor
source share