How to determine the max file upload limit in php

前端 未结 6 1440
渐次进展
渐次进展 2020-12-14 03:55

How can the file upload size allowed by php settings be determined withing a php script?

6条回答
  •  时光说笑
    2020-12-14 04:11

    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

提交回复
热议问题