How to determine the max file upload limit in php

前端 未结 6 1466
渐次进展
渐次进展 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:16

    Here's a single function, implementing the original idea from AoEmaster. The function returns integer (amount of bytes).

    function _GetMaxAllowedUploadSize(){
        $Sizes = array();
        $Sizes[] = ini_get('upload_max_filesize');
        $Sizes[] = ini_get('post_max_size');
        $Sizes[] = ini_get('memory_limit');
        for($x=0;$x

    If you want, you can combine it with the following function that renders the output as a human readable text.

    function _Byte2Size($bytes,$RoundLength=1) {
        $kb = 1024;         // Kilobyte
        $mb = 1024 * $kb;   // Megabyte
        $gb = 1024 * $mb;   // Gigabyte
        $tb = 1024 * $gb;   // Terabyte
    
        if($bytes < $kb) {
            if(!$bytes){
                $bytes = '0';
            }
            return (($bytes + 1)-1).' B';
        } else if($bytes < $mb) {
            return round($bytes/$kb,$RoundLength).' KB';
        } else if($bytes < $gb) {
            return round($bytes/$mb,$RoundLength).' MB';
        } else if($bytes < $tb) {
            return round($bytes/$gb,$RoundLength).' GB';
        } else {
            return round($bytes/$tb,$RoundLength).' TB';
        }
    }
    

    Use it this way:

    echo 'Max allowed upload size: '._Byte2Size(_GetMaxAllowedUploadSize());
    

    A result could be:

    Max allowed upload size: 500 MB

提交回复
热议问题