PHP has a configuration settings in php.ini which allows you to specify the maximum file upload size.
The values we are interested in here are post_max_size which controls the largest size of a post allowed – which is important since files are uploaded to a server using a POST, and the upload_max_filesize parameter, which sets how an actual file can be to upload.
To find the maximum file size we can upload, we need to find the smallest of these two values.
We can get the config setting using the PHP function ini_get() but there is a catch to this. It returns the size in the format of something similar to 2M, so we need to parse this number so we can have the actual size in bytes as a number.
The code below is based on some code I found in the comments section on the php.ini page of the PHP manual.
function convert_size_to_num($size) { //This function transforms the php.ini notation for numbers (like '2M') to an integer (2*1024*1024 in this case) $l = substr($size, -1); $ret = substr($size, 0, -1); switch(strtoupper($l)){ case 'P': $ret *= 1024; case 'T': $ret *= 1024; case 'G': $ret *= 1024; case 'M': $ret *= 1024; case 'K': $ret *= 1024; break; } return $ret; } function get_max_fileupload_size() { $max_upload_size = min(convert_size_to_num(ini_get('post_max_size')), $convert_size_to_num(ini_get('upload_max_filesize'))); return $max_upload_size; }