php PHP获取实际最大上传大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13076480/
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
PHP get actual maximum upload size
提问by Zulakis
When using
使用时
ini_get("upload_max_filesize");
it actually gives you the string specified in the php.ini file.
它实际上为您提供了 php.ini 文件中指定的字符串。
It is not good to use this value as a reference for the maximum upload size because
用这个值作为最大上传大小的参考是不好的,因为
- it is possible to use so-called shorthandbyteslike
1Mand so on which needs alot of additional parsing - when upload_max_filesize is for example
0.25M, it actually is ZERO, making the parsing of the value much harder once again - also, if the value contains any spaces like it is interpreted as ZERO by php, while it shows the value without spaces when using
ini_get
- 可以使用所谓的shorthandbytes之类的
1M,需要大量额外的解析 - 例如
0.25M,当upload_max_filesize 为0 时,它实际上是零,使值的解析再次变得更加困难 - 此外,如果该值包含任何空格,例如它被 php 解释为零,而它在使用时显示没有空格的值
ini_get
So, is there any way to get the value actually being used by PHP, besides the one reported by ini_get, or what is the best way to determinate it?
那么,除了由 报告的值之外,还有什么方法可以获得 PHP 实际使用的值ini_get,或者确定它的最佳方法是什么?
采纳答案by meustrus
Drupal has this implemented fairly elegantly:
Drupal 相当优雅地实现了这一点:
// Returns a file size limit in bytes based on the PHP upload_max_filesize
// and post_max_size
function file_upload_max_size() {
static $max_size = -1;
if ($max_size < 0) {
// Start with post_max_size.
$post_max_size = parse_size(ini_get('post_max_size'));
if ($post_max_size > 0) {
$max_size = $post_max_size;
}
// If upload_max_size is less, then reduce. Except if upload_max_size is
// zero, which indicates no limit.
$upload_max = parse_size(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
}
function parse_size($size) {
$unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size.
$size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size.
if ($unit) {
// Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
return round($size * pow(1024, stripos('bkmgtpezy', $unit[0])));
}
else {
return round($size);
}
}
The above functions are available anywhere in Drupal, or you can copy it and use it in your own project subject to the terms of the GPL license version 2 or later.
上述功能在 Drupal 的任何地方都可用,或者您可以复制它并在您自己的项目中使用它,但须遵守 GPL 许可证版本 2 或更高版本的条款。
As for parts 2 and 3 of your question, you will need to parse the php.inifile directly. These are essentially configuration errors, and PHP is resorting to fallback behaviors. It appears you can actually get the location of the loaded php.inifile in PHP, although trying to read from it may not work with basedir or safe-mode enabled:
至于问题的第 2 部分和第 3 部分,您需要php.ini直接解析文件。这些本质上是配置错误,PHP 正在诉诸回退行为。看起来您实际上可以php.ini在 PHP 中获取加载文件的位置,尽管尝试从中读取可能无法在启用 basedir 或安全模式的情况下工作:
$max_size = -1;
$post_overhead = 1024; // POST data contains more than just the file upload; see comment from @jlh
$files = array_merge(array(php_ini_loaded_file()), explode(",\n", php_ini_scanned_files()));
foreach (array_filter($files) as $file) {
$ini = parse_ini_file($file);
$regex = '/^([0-9]+)([bkmgtpezy])$/i';
if (!empty($ini['post_max_size']) && preg_match($regex, $ini['post_max_size'], $match)) {
$post_max_size = round($match[1] * pow(1024, stripos('bkmgtpezy', strtolower($match[2])));
if ($post_max_size > 0) {
$max_size = $post_max_size - $post_overhead;
}
}
if (!empty($ini['upload_max_filesize']) && preg_match($regex, $ini['upload_max_filesize'], $match)) {
$upload_max_filesize = round($match[1] * pow(1024, stripos('bkmgtpezy', strtolower($match[2])));
if ($upload_max_filesize > 0 && ($max_size <= 0 || $max_size > $upload_max_filesize) {
$max_size = $upload_max_filesize;
}
}
}
echo $max_size;
回答by Deckard
Here is the full solution. It takes care of all traps like the shorthand byte notation and also considers post_max_size:
这是完整的解决方案。它处理所有陷阱,如速记字节表示法,并考虑 post_max_size:
/**
* This function returns the maximum files size that can be uploaded
* in PHP
* @returns int File size in bytes
**/
function getMaximumFileUploadSize()
{
return min(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize')));
}
/**
* This function transforms the php.ini notation for numbers (like '2M') to an integer (2*1024*1024 in this case)
*
* @param string $sSize
* @return integer The value in bytes
*/
function convertPHPSizeToBytes($sSize)
{
//
$sSuffix = strtoupper(substr($sSize, -1));
if (!in_array($sSuffix,array('P','T','G','M','K'))){
return (int)$sSize;
}
$iValue = substr($sSize, 0, -1);
switch ($sSuffix) {
case 'P':
$iValue *= 1024;
// Fallthrough intended
case 'T':
$iValue *= 1024;
// Fallthrough intended
case 'G':
$iValue *= 1024;
// Fallthrough intended
case 'M':
$iValue *= 1024;
// Fallthrough intended
case 'K':
$iValue *= 1024;
break;
}
return (int)$iValue;
}
This is an error-free version of this source: http://www.smokycogs.com/blog/finding-the-maximum-file-upload-size-in-php/.
这是此来源的无错误版本:http: //www.smokycogs.com/blog/finding-the-maximum-file-upload-size-in-php/。
回答by jcampbell1
This is what I use:
这是我使用的:
function asBytes($ini_v) {
$ini_v = trim($ini_v);
$s = [ 'g'=> 1<<30, 'm' => 1<<20, 'k' => 1<<10 ];
return intval($ini_v) * ($s[strtolower(substr($ini_v,-1))] ?: 1);
}
回答by Zulakis
Looks like it isn't possible.
看来是不可能了。
Because of this, I am going to continue using this code:
因此,我将继续使用此代码:
function convertBytes( $value ) {
if ( is_numeric( $value ) ) {
return $value;
} else {
$value_length = strlen($value);
$qty = substr( $value, 0, $value_length - 1 );
$unit = strtolower( substr( $value, $value_length - 1 ) );
switch ( $unit ) {
case 'k':
$qty *= 1024;
break;
case 'm':
$qty *= 1048576;
break;
case 'g':
$qty *= 1073741824;
break;
}
return $qty;
}
}
$maxFileSize = convertBytes(ini_get('upload_max_filesize'));
Originally from thishelpful php.net comment.
最初来自这个有用的 php.net 评论。
STILL OPEN TO ACCEPT BETTER ANSWERS
仍然愿意接受更好的答案
回答by Thomas
I don't think so, at least not in the way you have defined it. There are so many other factors that come into consideration for maximum file upload size, most notably the connection speed of the user as well as the timeout setting for the web server as well as the PHP process(es).
我不这么认为,至少不是你定义的方式。最大文件上传大小还有很多其他因素需要考虑,最显着的是用户的连接速度以及 Web 服务器的超时设置以及 PHP 进程。
A more useful metric for you might be to decide what is a reasonable maximum file size for the types of files you expect to receive for a given input. Make the decision on what is reasonable for your use case and set a policy around that.
对您来说更有用的指标可能是确定对于给定输入期望接收的文件类型的合理最大文件大小是多少。决定什么对您的用例是合理的,并围绕它制定政策。
回答by Martin
Well you can always use this syntax, which will give you correct numbers from PHP ini file:
那么你总是可以使用这个语法,它会给你来自 PHP ini 文件的正确数字:
$maxUpload = (int)(ini_get('upload_max_filesize'));
$maxPost = (int)(ini_get('post_max_size'));
Mart
市场

