php 如何确定php中的最大文件上传限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2840755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:50:59  来源:igfitidea点击:

How to determine the max file upload limit in php

php

提问by HyperCas

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

如何使用 php 脚本确定 php 设置允许的文件上传大小?

回答by HyperCas

The upload is limited by three options: upload_max_filesize, post_max_sizeand memory_limit. Your upload is only done if it doesn't exeed one of them.

上传受三个选项的限制:upload_max_filesizepost_max_sizememory_limit。您的上传只有在没有超过其中之一的情况下才会完成。

The ini_get()function provides you with a short hand of the limit and should be converted first. Thx to AoEmasterfor this.

所述ini_get()函数为您提供了极限的短手和应首先被转换。为此向AoEmaster 致谢

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.kavtheitroad.com/2010/02/php-get-the-file-uploading-limit-max-file-size-allowed-to-upload.html

来源:http: //www.kavtheitroad.com/2010/02/php-get-the-file-uploading-limit-max-file-size-allowed-to-upload.html

回答by Gumbo

Use ini_getto get the current configuration value:

使用ini_get来获得当前的配置值:

ini_get('upload_max_filesize')

回答by Renish Khunt

You can also change that size at runtime using the .htaccess file without the need to change your php.ini file

您还可以在运行时使用 .htaccess 文件更改该大小,而无需更改 php.ini 文件

php_value upload_max_filesize 1224M
php_value post_max_size 1224M
php_value max_execution_time 3000
php_value max_input_time 3000

copy that code and put your file, then store that file with index file then you run your project you also capable to upload 1GB file

复制该代码并放置您的文件,然后将该文件与索引文件一起存储,然后运行您的项目,您还可以上传 1GB 文件

for more detail read this article

有关更多详细信息,请阅读这篇文章

回答by AoEmaster

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 get_upload_max_filesize()
{
    $max_upload = return_bytes(ini_get('upload_max_filesize'));
    $max_post = return_bytes(ini_get('post_max_size'));
    return min($max_upload, $max_post, $memory_limit);
}

回答by Sebastian Unterberg

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

这是一个单一的功能,实现了 AoEmaster 的原始想法。该函数返回整数(字节数)。

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<count($Sizes);$x++){
        $Last = strtolower($Sizes[$x][strlen($Sizes[$x])-1]);
        if($Last == 'k'){
            $Sizes[$x] *= 1024;
        } elseif($Last == 'm'){
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
        } elseif($Last == 'g'){
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
        } elseif($Last == 't'){
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
            $Sizes[$x] *= 1024;
        }
    }
    return min($Sizes);
}

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

最大允许上传大小:500 MB

回答by Liju

You can set Maximum file upload size using ini_set()

您可以使用 ini_set() 设置最大文件上传大小

  ini_set('post_max_size', '64M');
  ini_set('upload_max_filesize', '64M');

or using the .htaccess file

或使用 .htaccess 文件

php_value upload_max_filesize 
php_value post_max_size