Определение максимального размера загружаемого файла в PHP

Размер файла, который может быть загружен на сервер при текущих настройках PHP, зависит от трёх параметров php.ini(upload_max_filesize, post_max_size, memory_limit) и не может быть больше каждого из них.

/**
* Detects max size of file can be uploaded to server
*
* Based on php.ini parameters "upload_max_filesize", "post_max_size" &
* "memory_limit". Valid for single file upload form. Respects floating point
* values like "1.5G and special values of memory_limit = -1 and
* post_max_size = 0 that treats as unlimited.
*
* @throws Exception
* @return int|float	Max file size in bytes
*/
function maxUploadFileSize()
{
    /**
    * Converts shorthands like "2M" or "512K" to bytes
    * @param int $size
    * @return int|float
    * @throws Exception
    */
    $normalize = function($size) {
        if (preg_match(\'/^(-?[\d\.]+)(|[KMG])$/i\', $size, $match)) {
            $pos = array_search($match[2], array("", "K", "M", "G"));
            $size = $match[1] * pow(1024, $pos);
        } else {
            throw new Exception("Failed to normalize memory size {$size} (unknown format)");
        }
        return $size;
    };
    $limits = array();
    $limits[] = $normalize(ini_get("upload_max_filesize"));
    if (($max_post = $normalize(ini_get("post_max_size"))) != 0) {
        $limits[] = $max_post;
    }
    if (($memory_limit = $normalize(ini_get("memory_limit"))) != -1) {
        $limits[] = $memory_limit;
    }
    $maxFileSize = min($limits);
    return $maxFileSize;
}

Функция для форматированного вывода размера файла в мегабайтах

/**
* Converts size in bytes into human-friendly format in megabytes (MiB)
* and trims redundant zero decimals
*
* @param int|float $size		Size of file or memory in bytes
* @param int $maxDecimals		OPTIONAL	Amount of max decimal digits
* @param string $mbSuffix		OPTIONAL	MB-suffix (may be used for i18n)
* @return string
*/
function formatSizeInMb($size, $maxDecimals = 3, $mbSuffix = "Mb")
{
    $mbSize = round($size / 1024 / 1024, $maxDecimals);
    return preg_replace("/\\.0+$/", "", $mbSize) . $mbSuffix;
}

  1. PHP
  2. Примеры