63

使用时

ini_get("upload_max_filesize");

它实际上为您提供了 php.ini 文件中指定的字符串。

将此值用作最大上传大小的参考并不好,因为

  • 可以使用所谓的shorthandbytes之类1M的,需要大量额外的解析
  • 例如,当 upload_max_filesize 为 时0.25M,它实际上是零,再次使值的解析变得更加困难
  • 此外,如果该值包含任何空格,例如它被 php 解释为零,而它在使用时显示没有空格的值ini_get

那么,除了 报告的值之外,还有什么方法可以获取 PHP 实际使用的值ini_get,或者确定它的最佳方法是什么?

4

6 回答 6

72

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);
  }
}

上述功能在 Drupal 的任何地方都可用,或者您可以根据 GPL 许可版本 2 或更高版本的条款将其复制并在您自己的项目中使用。

至于问题的第 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;
于 2014-08-18T19:50:39.337 回答
42

这是完整的解决方案。它处理所有陷阱,如速记字节表示法,还考虑 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;
}      
于 2014-03-19T08:44:36.310 回答
8

这就是我使用的:

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);
}
于 2013-10-24T15:36:01.723 回答
5

看来不可能了。

因此,我将继续使用此代码:

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'));

最初来自这个有用的 php.net 评论。

仍然愿意接受更好的答案

于 2012-11-04T22:16:37.497 回答
0

我不这么认为,至少不是你定义的方式。对于最大文件上传大小,还有很多其他因素需要考虑,最值得注意的是用户的连接速度以及 Web 服务器的超时设置以及 PHP 进程。

对您来说更有用的指标可能是确定对于给定输入您希望接收的文件类型的合理最大文件大小。决定什么对您的用例是合理的,并围绕它制定政策。

于 2012-10-25T20:19:45.777 回答
-1

好吧,您总是可以使用这种语法,它会从 PHP ini 文件中为您提供正确的数字:

$maxUpload      = (int)(ini_get('upload_max_filesize'));
$maxPost        = (int)(ini_get('post_max_size'));

市场

于 2013-10-14T20:36:24.420 回答