PHP警告:n字节的POST Content-Length超出了第0行中Unknown中的3145728字节的限制 [英] PHP Warning: POST Content-Length of n bytes exceeds the limit of 3145728 bytes in Unknown on line 0

查看:181
本文介绍了PHP警告:n字节的POST Content-Length超出了第0行中Unknown中的3145728字节的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶在错误日志中找到上述错误,因为我以为我已经完成了必要的工作来捕捉我的PHP脚本中的错误:

I am quite surprise to find the above-mentioned error in my error log because I thought I have already done the necessary work to catch the error in my PHP script:

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脚本中,相关设置为:

In my PHP.ini script, the relevant settings are:

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

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

I understand that the 3M is equivalent to 3145728 bytes and that this is what that is triggering the error. If the file size is above 500k but less than 3M, the PHP script would be able to run as per normal, issuing the error message in $msg as per case 1.

当帖子大小超过post_max_size但仍在内存限制之内时,如何捕获此错误,而不是让脚本突然终止并显示PHP警告?我看过类似的问题此处

How do I catch this error instead of letting the script terminate abruptly with a PHP warning when post size exceeds post_max_size but still well within the memory limit? I have looked at similar questions here, here and here, but couldn't find an answer.

推荐答案

找到了一种不直接处理错误的替代解决方案.以下代码是由软件工程师Andrew Curioso在其中编写的博客:

Found an alternative solution that does not deal with the error directly. The following code is written by a software engineer Andrew Curioso in his 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的超全局数组将变为空.因此,通过测试这些内容并确认使用POST方法发送了一些内容,可以推断出发生了这种错误.

As explained in his article, when the post size exceeds post_max_size, the super global arrays of $_POST and $_FILES will become empty. So, by testing for these and by confirming that there is some content being sent using the POST method, it can be deduced that such an error has occurred.

实际上有一个类似的问题,此处,我没能早点找到.

There is actually a similar question here, which I didn't manage to find earlier.

这篇关于PHP警告:n字节的POST Content-Length超出了第0行中Unknown中的3145728字节的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆