Skip to content

Instantly share code, notes, and snippets.

@akingdom
Last active March 25, 2022 14:25
Show Gist options
  • Save akingdom/116f1420aa720c4f6498425279f70b4f to your computer and use it in GitHub Desktop.
Save akingdom/116f1420aa720c4f6498425279f70b4f to your computer and use it in GitHub Desktop.
// Returns the minimum download limit specified in the current php.ini file.
//
// By Andrew Kingdom
// MIT license
function max_upload_size() {
$max_size = PHP_INT_MAX;
$post_overhead = 2048; // Reserve 2k for non-file data in the POST.
$tmp = shorthand_bytes(ini_get('upload_max_filesize'));
if ($tmp > 0 && $tmp < $max_size) $max_size = $tmp;
$tmp = shorthand_bytes(ini_get('post_max_size'));
if ($tmp > 0 && $tmp < $max_size) $max_size = $tmp;
$tmp = shorthand_bytes(ini_get('memory_limit'));
if ($tmp > 0 && $tmp < $max_size) $max_size = $tmp;
if ($max_size === PHP_INT_MAX) $max_size = 0; // no upload limit, use 0, for safety -- comment line out if not required.
return $max_size;
}
function shorthand_bytes($str) {
$str = trim($str);
$len = strlen($str);
if ($len == 0) return 0;
$last = strtolower($str[$len-1]);
return round(floatval($str) * pow(1024, stripos('bkmgtpezy', $last)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment