PHP: Get the File Uploading Limit – Max File Size Allowed to Upload

PHP file upload max size is determined by 3 configuration values in php.ini, namely upload_max_filesize, post_max_size and memory_limit. You can get the maximum file size allowed in uploading by this snippet:

$max_upload = (int)(ini_get('upload_max_filesize'));
$max_post = (int)(ini_get('post_max_size'));
$memory_limit = (int)(ini_get('memory_limit'));
$upload_mb = min($max_upload, $max_post, $memory_limit);

Wherein $upload_mb is the maximum file size allowed for upload in MB. It’s the smallest of the 3 values. Just display this value beside the file upload control so the user knows the limit before choosing the file.

16 thoughts on “PHP: Get the File Uploading Limit – Max File Size Allowed to Upload”

  1. From PHP manual:

    Note: When querying memory size values

    Many ini memory size values, such as upload_max_filesize, are stored in the php.ini file in shorthand notation. ini_get() will return *the exact string stored in the php.ini file* and NOT its integer equivalent. Attempting normal arithmetic functions on these values will not have otherwise expected results.

    Since not everyone will use the same notation for all three values, it’s better to convert all of them to bytes first, before finding min value.

  2. upload_max_filesize is for _one_ file. When uploading multiple files at same time you can upload several files who together are bigger than the upload_max_filesize.

    I believe this would be more interesting:

    $max_upload = (int)(ini_get(‘upload_max_filesize’));
    $max_post = (int)(ini_get(‘post_max_size’));
    $max_memory = (int)(ini_get(‘memory_limit’));
    $file_limit = min($max_upload, $max_post, $max_memory);
    $total_limit = min($max_post, $max_memory);

  3. You forgot, that in case if
    upload_max_filesize – 900K
    and post_max_size – 2,3M
    You’ll get 900M
    Or in case when upload_max_filesize – 5,1M and post_max_size – 5,7M you’ll get 5.

    1. You’re absolutely right! The topic starter didn’t rely on this cases. I wrote my own function that returns calculated max size of uploading file. It takes into account all your notes. Here it’s code:


      /**
      * Detects max size of file cab be uploaded to server
      *
      * Based on php.ini parameters "upload_max_filesize", "post_max_size" &
      * "memory_limit". Valid for single file upload form. May be used
      * as MAX_FILE_SIZE hidden input or to inform user about max allowed file size.
      *
      * @return int Max file size in bytes
      */
      function detectMaxUploadFileSize()
      {
      /**
      * Converts shorthands like "2M" or "512K" to bytes
      *
      * @param $size
      * @return mixed
      */
      $normalize = function($size) {
      if (preg_match('/^([\d\.]+)([KMG])$/i', $size, $match)) {
      $pos = array_search($match[2], array("K", "M", "G"));
      if ($pos !== false) {
      $size = $match[1] * pow(1024, $pos + 1);
      }
      }
      return $size;
      };
      $max_upload = $normalize(ini_get('upload_max_filesize'));
      $max_post = $normalize(ini_get('post_max_size'));
      $memory_limit = $normalize(ini_get('memory_limit'));
      $maxFileSize = min($max_upload, $max_post, $memory_limit);
      return $maxFileSize;
      }

      — Paul Melekhov aKa gugglegum, Russia.

    2. I have an important note to my previous message with function detectMaxUploadFileSize(). This function does not respect some special php.ini values that treats as unlimited values. For example:
      memory_limit = -1
      post_max_size = 0

      My function need to be rewrited, but I have no time at this moment. You can contact me via email gugglegum at gmail.com

      1. as i am going to use the snippet Paul kindly shared i hereby share modified version
        /**
        * Detects max size of file cab be uploaded to server
        *
        * Based on php.ini parameters “upload_max_filesize”, “post_max_size” &
        * “memory_limit”. Valid for single file upload form. May be used
        * as MAX_FILE_SIZE hidden input or to inform user about max allowed file size.
        * RULE memory_limit > post_max_size > upload_max_filesize
        * http://php.net/manual/en/ini.core.php : 128M > 8M > 2M
        * Sets max size of post data allowed. This setting also affects file upload.
        * To upload large files, this value must be larger than upload_max_filesize.
        * If memory limit is enabled by your configure script, memory_limit also
        * affects file uploading. Generally speaking, memory_limit should be larger
        * than post_max_size. When an integer is used, the value is measured in bytes.
        * Shorthand notation, as described in this FAQ, may also be used. If the size
        * of post data is greater than post_max_size, the $_POST and $_FILES
        * superglobals are empty. This can be tracked in various ways, e.g. by passing
        * the $_GET variable to the script processing the data, i.e.
        * , and then checking
        * if $_GET[‘processed’] is set.
        * memory_limit > post_max_size > upload_max_filesize
        * @author Paul Melekhov edited by lostinscope
        * @return int Max file size in bytes
        */
        function detectMaxUploadFileSize(){
        /**
        * Converts shorthands like “2M” or “512K” to bytes
        *
        * @param $size
        * @return mixed
        */
        $normalize = function($size) {
        if (preg_match(‘/^([\d\.]+)([KMG])$/i’, $size, $match)) {
        $pos = array_search($match[2], array(“K”, “M”, “G”));
        if ($pos !== false) {
        $size = $match[1] * pow(1024, $pos + 1);
        }
        }
        return $size;
        };
        $max_upload = $normalize(ini_get(‘upload_max_filesize’));

        $max_post = (ini_get(‘post_max_size’) == 0) ?
        function(){throw new Exception(‘Check Your php.ini settings’);}
        : $normalize(ini_get(‘post_max_size’));

        $memory_limit = (ini_get(‘memory_limit’) == -1) ?
        $max_post : $normalize(ini_get(‘memory_limit’));

        if($memory_limit < $max_post || $memory_limit < $max_upload)
        return $memory_limit;

        if($max_post < $max_upload)
        return $max_post;

        $maxFileSize = min($max_upload, $max_post, $memory_limit);
        return $maxFileSize;
        }

Comments are closed.

Scroll to Top