jjzjj

PHP 警告 : POST Content-Length of n bytes exceeds the limit of 3145728 bytes in Unknown on line 0

coder 2024-01-01 原文

我很惊讶地在我的错误日志中发现上述错误,因为我认为我已经完成了必要的工作来捕获我的 PHP 脚本中的错误:

if ($_FILES['image']['error'] == 0)
{
 // go ahead to process the image file
}
else
{
 // determine the error
 switch($_FILES['image']['error'])
 {
  case "1":
  $msg = "Uploaded file exceeds the upload_max_filesize directive in php.ini.";
  break;
  ....
 }
}

在我的PHP.ini脚本中,相关的设置是:

memory_limit = 128M
post_max_size = 3M
upload_max_filesize = 500K

我知道 3M 相当于 3145728 字节,这就是触发错误的原因。如果文件大小大于 500k 但小于 3M,PHP 脚本将能够正常运行,根据 case 1$msg 中发出错误消息。

当帖子大小超过 post_max_size 但仍在内存限制内时,我如何捕获此错误而不是让脚本突然终止并发出 PHP 警告?我看过类似的问题here , herehere , 但找不到答案。

最佳答案

找到了一个不直接处理错误的替代解决方案。以下代码由软件工程师 Andrew Curioso 在他的 blog 中编写。 :

if($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) &&
     empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0)
{
  $displayMaxSize = ini_get('post_max_size');

  switch(substr($displayMaxSize,-1))
  {
    case 'G':
      $displayMaxSize = $displayMaxSize * 1024;
    case 'M':
      $displayMaxSize = $displayMaxSize * 1024;
    case 'K':
       $displayMaxSize = $displayMaxSize * 1024;
  }

  $error = 'Posted data is too large. '.
           $_SERVER[CONTENT_LENGTH].
           ' bytes exceeds the maximum size of '.
           $displayMaxSize.' bytes.';
}

正如他的文章中所解释的,当帖子大小超过 post_max_size 时,$_POST$_FILES 的 super 全局数组将变为空.因此,通过测试这些并确认有一些内容正在使用POST方法发送,可以推断出发生了这样的错误。

其实还有一个类似的问题here ,我之前没能找到。

关于PHP 警告 : POST Content-Length of n bytes exceeds the limit of 3145728 bytes in Unknown on line 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11738949/

有关PHP 警告 : POST Content-Length of n bytes exceeds the limit of 3145728 bytes in Unknown on line 0的更多相关文章

随机推荐