Return to Snippet

Revision: 50720
at August 31, 2011 07:52 by kendsnyder


Initial Code
<?php

// TESTED:
function getMaxFileUploadSize() {
	$maxPost = ini_get('post_max_size'); 
	if     (substr($maxPost,-1) == 'K') $maxPost = substr($maxPost,0,-1) * 1024;
	elseif (substr($maxPost,-1) == 'M') $maxPost = substr($maxPost,0,-1) * 1024 * 1024;
	elseif (substr($maxPost,-1) == 'G') $maxPost = substr($maxPost,0,-1) * 1024 * 1024 * 1024;
	$maxUpload = ini_get('upload_max_filesize');
	if     (substr($maxUpload,-1) == 'K') $maxUpload = substr($maxUpload,0,-1) * 1024;
	elseif (substr($maxUpload,-1) == 'M') $maxUpload = substr($maxUpload,0,-1) * 1024 * 1024;
	elseif (substr($maxUpload,-1) == 'G') $maxUpload = substr($maxUpload,0,-1) * 1024 * 1024 * 1024;
	$maxFileSize = min($maxPost, $maxUpload);
	$maxFileSize = floor($maxFileSize / 1024 / 1024) . 'MB';
	return $maxFileSize;
}

// UNTESTED
function getMaxFileUploadSize() {
	$maxPost = _interpretKMG(ini_get('post_max_size')); 
	$maxUpload = _interpretKMG(ini_get('upload_max_filesize'));
	$maxFileSize = min($maxPost, $maxUpload);
	$maxFileSize = floor($maxFileSize / 1024 / 1024) . 'MB';
}

function _interpretKMG($text) {
	if (preg_match('/^\d+$/', $text) {
		return $text;
	}
	$num = substr($text,0,-1);
	$suffix = substr($text,-1);
	switch ($suffix) {
		case 'G': $num *= 1024;
		case 'M': $num *= 1024;
		case 'K': $num *= 1024;
	}
	return $num;
}

Initial URL


Initial Description


Initial Title
Get max upload size as set in php.ini

Initial Tags


Initial Language
PHP