PHP get actual maximum upload size

PHP get actual maximum upload size

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why ini_get("upload_max_filesize") Doesn't Tell the Whole Story

When building a file upload feature, developers often reach for $maxSize = ini_get('upload_max_filesize'); to determine how large a file their application can accept. While this function seems straightforward, relying on its output directly can lead to subtle bugs and parsing nightmares. The value returned by ini_get() is simply the raw string defined in your php.ini configuration file. It does not represent the actual numeric limit PHP enforces during runtime.

Using this string as a reference introduces immediate complexity because of shorthand byte units. If your configuration contains 2M, 10k, or 50G, ini_get() returns that exact string. To use this in memory calculations, you must implement custom parsing logic to convert these units into bytes. This adds unnecessary overhead and increases the surface area for errors, especially when dealing with edge cases like uppercase/lowercase unit mismatches or invalid shorthand syntax.

A more insidious issue arises from how PHP interprets specific values. If upload_max_filesize is set to 0, 0M, or even 0.25M, the configuration effectively disables file uploads entirely, making the limit zero bytes. However, ini_get() might return a string like "0.25M" without immediately signaling that this equates to a hard block. Attempting to parse "0.25M" mathematically is prone to errors unless you explicitly handle the conversion logic before checking if the result is greater than zero.

Furthermore, whitespace handling creates another pitfall. The php.ini file often allows spaces after the value, such as upload_max_filesize = 10M. While human-readable, standard string parsing functions might struggle if they do not explicitly trim the input first. In some PHP configurations or when reading from memory-based directives (via .htaccess), the behavior can differ slightly from file-based settings, potentially causing discrepancies between what ini_get() reports and what the engine actually accepts.

The Reliable Alternative: php://input Limitations

To bypass these parsing headaches, the most robust approach is to determine the effective limit dynamically rather than statically reading the configuration string. However, PHP does not provide a direct API function that returns the parsed integer value of upload_max_filesize.

The standard recommendation in the community and documentation is to check the max_execution_time or parse the php.ini carefully using the ini_parse_all_ini_file() approach available in newer PHP versions (8.1+), though this still requires manual unit conversion logic.

If you need to validate a file size against the actual limit before attempting an upload, you must parse the shorthand units yourself. A helper function can abstract this complexity:

function convertSizeToBytes(string $size): int
{
    $unit = strtoupper(substr($size, -1));
    if ($unit == 'M') return (int)substr($size, 0, -1) * 1024 * 1024;
    if ($unit == 'G') return (int)substr($size, 0, -1) * 1024 * 1024 * 1024;
    if ($unit == 'K') return (int)substr($size, 0, -1) * 1024;
    
    // Handle raw numbers or zero cases
    return (int)$size;
}

$maxUploadSize = convertSizeToBytes(trim(ini_get('upload_max_filesize')));

This function handles the shorthand units explicitly. Note that if ini_get() returns "0", this will correctly yield 0 bytes, preventing attempts to parse invalid unit strings like "0M".

Trade-offs and Best Practices

Relying on static configuration checks is risky because the actual limit can be overridden by several factors:

  1. post_max_size: The value returned by upload_max_filesize must never exceed post_max_size. If post_max_size is smaller, uploads will fail regardless of the upload setting.
  2. Memory Limits: PHP's memory limit (memory_limit) also constrains uploads. A file that fits within upload_max_filesize might still cause a crash if it exceeds available RAM.
  3. Web Server Config: In Apache or Nginx setups, .htaccess files can override php.ini settings. If a developer edits php.ini to allow 50MB but the web server runs with an older config where uploads are disabled, ini_get() will reflect the current runtime setting, but the effective limit might be lower due to other constraints.

For high-stakes applications, always validate the incoming file size against the parsed integer value of upload_max_filesize AND post_max_size. Additionally, consider implementing a soft limit in your application code that is slightly lower than the server's maximum to prevent hitting PHP errors under load.

If you are working within the Laravel ecosystem or similar frameworks, framework-specific configuration methods often wrap these raw checks to provide cleaner abstractions. However, understanding the underlying mechanics ensures you don't encounter silent failures when deploying to different environments where php.ini defaults vary. Always assume that ini_get() provides a string, not a number, until you have explicitly converted it.

Our network