Example #1
0
/**
 * @brief Returns a file size limit in bytes based on the PHP upload_max_filesize and post_max_size
 * @return int
 */
function fileUploadMaxSize()
{
    static $max_size;
    if (!isset($max_size)) {
        // Start with post_max_size.
        $max_size = parseSize(ini_get('post_max_size'));
        // If upload_max_size is less, then reduce. Except if upload_max_size is
        // zero, which indicates no limit.
        $upload_max = parseSize(ini_get('upload_max_filesize'));
        if ($upload_max > 0 && $upload_max < $max_size) {
            $max_size = $upload_max;
        }
    }
    return $max_size;
}
/**
 * Returns a file size limit in bytes based on the PHP upload_max_filesize and post_max_size
 * @return Ambigous <number, unknown>
 */
function sy_get_max_upload_size($humanReadable = true, $decimals = 2)
{
    function parseSize($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);
        }
    }
    static $max_size = -1;
    if ($max_size < 0) {
        // Start with post_max_size.
        $max_size = parseSize(ini_get('post_max_size'));
        // If upload_max_size is less, then reduce. Except if upload_max_size is
        // zero, which indicates no limit.
        $upload_max = parseSize(ini_get('upload_max_filesize'));
        if ($upload_max > 0 && $upload_max < $max_size) {
            $max_size = $upload_max;
        }
    }
    $sz = 'BKMGTPEZY';
    $factor = floor((strlen($max_size) - 1) / 3);
    return $humanReadable ? sprintf("%.{$decimals}f", $max_size / pow(1024, $factor)) . @$sz[$factor] : $max_size;
}