PHP Get Actual Maximum Upload Size

PHP get actual maximum upload size

Drupal has this implemented fairly elegantly:

// Returns a file size limit in bytes based on the PHP upload_max_filesize
// and post_max_size
function file_upload_max_size() {
static $max_size = -1;

if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = parse_size(ini_get('post_max_size'));
if ($post_max_size > 0) {
$max_size = $post_max_size;
}

// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = parse_size(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
}

function parse_size($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}

The above functions are available anywhere in Drupal, or you can copy it and use it in your own project subject to the terms of the GPL license version 2 or later.

As for parts 2 and 3 of your question, you will need to parse the php.ini file directly. These are essentially configuration errors, and PHP is resorting to fallback behaviors. It appears you can actually get the location of the loaded php.ini file in PHP, although trying to read from it may not work with basedir or safe-mode enabled:

$max_size = -1;
$post_overhead = 1024; // POST data contains more than just the file upload; see comment from @jlh
$files = array_merge(array(php_ini_loaded_file()), explode(",\n", php_ini_scanned_files()));
foreach (array_filter($files) as $file) {
$ini = parse_ini_file($file);
$regex = '/^([0-9]+)([bkmgtpezy])$/i';
if (!empty($ini['post_max_size']) && preg_match($regex, $ini['post_max_size'], $match)) {
$post_max_size = round($match[1] * pow(1024, stripos('bkmgtpezy', strtolower($match[2])));
if ($post_max_size > 0) {
$max_size = $post_max_size - $post_overhead;
}
}
if (!empty($ini['upload_max_filesize']) && preg_match($regex, $ini['upload_max_filesize'], $match)) {
$upload_max_filesize = round($match[1] * pow(1024, stripos('bkmgtpezy', strtolower($match[2])));
if ($upload_max_filesize > 0 && ($max_size <= 0 || $max_size > $upload_max_filesize) {
$max_size = $upload_max_filesize;
}
}
}

echo $max_size;

How to determine the max file upload limit in php

The upload is limited by three options: upload_max_filesize, post_max_size and memory_limit. Your upload is only done if it doesn't exeed one of them.

The ini_get() function provides you with a short hand of the limit and should be converted first. Thx to AoEmaster for this.

function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last)
{
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}

function max_file_upload_in_bytes() {
//select maximum upload size
$max_upload = return_bytes(ini_get('upload_max_filesize'));
//select post limit
$max_post = return_bytes(ini_get('post_max_size'));
//select memory limit
$memory_limit = return_bytes(ini_get('memory_limit'));
// return the smallest of them, this defines the real limit
return min($max_upload, $max_post, $memory_limit);
}

Source: http://www.kavoir.com/2010/02/php-get-the-file-uploading-limit-max-file-size-allowed-to-upload.html

Detect if uploaded file is too large

You could check the $_SERVER['CONTENT_LENGTH']:

// check that post_max_size has not been reached
// convert_to_bytes is the function turn `5M` to bytes because $_SERVER['CONTENT_LENGTH'] is in bytes.
if (isset($_SERVER['CONTENT_LENGTH'])
&& (int) $_SERVER['CONTENT_LENGTH'] > convert_to_bytes(ini_get('post_max_size')))
{
// ... with your logic
throw new Exception('File too large!');
}

Import file size limit in PHPMyAdmin

You probably didn't restart your server ;)

Or you modified the wrong php.ini.

Or you actually managed to do both ^^

PHP Upload File Type, Size and Existence?

To Check file extension

$tmpFilePath = $_FILES['upload']['tmp_name'][$i];

$imageFileType = pathinfo($tmpFilePath,PATHINFO_EXTENSION);

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Only JPG, JPEG, PNG & GIF files are allowed.";
}

To check file size

if ($_FILES["upload"]["size"][$i] > 500000) {
echo "Sorry, your file is too large.";
}

To check is file received

if($_FILES['upload']['tmp_name'][$i]!=""){

}


Related Topics



Leave a reply



Submit