php 如何防止警告:POST Content-Length 和内存大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21704930/
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
How to prevent Warning: POST Content-Length and memory size
提问by jave.web
Currently when user uploads a photo the page says "Warning: POST Content-Length of XXXbytes exceeds the limit of 21000000 bytes in Unknown on line 0".
当前,当用户上传照片时,页面会显示“警告:XXX字节的POST 内容长度超出第 0 行未知中 21000000 字节的限制”。
I know what that means and I am NOT looking for the solultions like the increasing the max_upload values or even memory_size_limit... Because users may and users willupload terabytes of nonsense even if you explicitly tell them only max 20MB files and only images are allowed.
我知道这意味着什么,我不是在寻找诸如增加 max_upload 值甚至 memory_size_limit 之类的解决方案......因为即使您明确告诉他们最多只能上传 20MB 文件并且只允许图像,用户也可能会上传数 TB 的废话.
I am looking for a solution on:
我正在寻找解决方案:
- How to prevent this warning(s) to even happen?
OR at least: - How to prevent displaying of this warning(s)?
- 如何防止这种警告甚至发生?
或者至少: - 如何防止显示此警告?
EDIT:PLEASE READ ! - Please understand that of course I am handling the error/warningafter (since line 1) , problem is this happens on a virtual "line 0"that is why I need to hide the error or prevent it to raise - because I cant put any code before the place where the error happens.
编辑:请阅读!- 请理解,当然我在处理错误/警告之后(从第 1 行开始),问题是这发生在虚拟的“第 0 行”上,这就是为什么我需要隐藏错误或防止它引发 - 因为我不能把发生错误的地方之前的任何代码。
EDIT2:Finally after a very long research and digging I got an idea - it worked - see my own answer.
EDIT2:经过长时间的研究和挖掘,我终于有了一个想法——它奏效了——看看我自己的答案。
回答by jave.web
So after searching instead of working today I finally got an idea how to solve this, it worked, and even didnt cause much damage. But please first understand what you are doing, before doing it. :) As I suggested in one of my comments it is really possible to turn off PHP errors in .htacess - just turn off the PHP startup warnings.
因此,经过搜索而不是今天的工作,我终于知道如何解决这个问题,它起作用了,甚至没有造成太大的损害。但是请先了解您在做什么,然后再做。:) 正如我在我的评论之一中建议的那样,确实可以关闭 .htacess 中的 PHP 错误 - 只需关闭 PHP 启动警告即可。
Before you apply the solution:
在应用解决方案之前:
Note that:after you insert this code to your .htaccess you won't be able to see anystartup error
请注意:将此代码插入到 .htaccess 后,您将看不到任何启动错误
Also note that:there are more start up errors on line "0" than this one.
另请注意:“0”行上的启动错误比这多。
Do before:Before you do this you should prepare your script in the way that it should check the uploaded content size and give user a proper information message. The fact that the warning doesnt show DOES NOTmean that you should do nothing about it. It means the EXACT oposite - you should do all that you can to make something at least near-equal to the warning raise - check, double check if you can, handle error and raise your own error message.
之前做:在你这样做之前,你应该准备你的脚本,它应该检查上传的内容大小并给用户一个正确的信息消息。警告未显示的事实并不意味着您不应该对此采取任何措施。这意味着完全相反 - 您应该尽一切努力使某些事情至少接近警告引发 - 检查,如果可以,请仔细检查,处理错误并提出您自己的错误消息。
Add this to your .htaccess:
将此添加到您的 .htaccess 中:
php_flag display_startup_errors off
php_flag display_startup_errors off
It is not that evil as it seems to be:
它并不像看起来那么邪恶:
Please note that this turns off startup errors only.
So all the regular PHP errors/warnings/notices stays ON :)
请注意,这仅关闭启动错误。
所以所有常规的 PHP 错误/警告/通知都保持开启 :)
Even XAMPP's PHP "itself" recommends it for production:
甚至 XAMPP 的 PHP“本身”也推荐它用于生产:
The php.inifile literaly says:
该php.ini文件字面意思是:
; display_startup_errors
; Default Value: Off
; Development Value: On
; Production Value: Off
PS:"startup error" seems to be those errors beforePHP script is executed itself - these errors are usually trying to "persuade" you that they are on the line 0.
PS:“启动错误”似乎是在PHP 脚本本身执行之前的那些错误- 这些错误通常试图“说服”你他们在 0 行上。
Thanks to my idea and this answer: How to disable notice and warning in PHP within .htaccess file?
感谢我的想法和这个答案:如何在 .htaccess 文件中禁用 PHP 中的通知和警告?
EDIT:As this is a php_flag setting, you can of course also set it by default in your php.ini if you have custom instalation of PHP :)
编辑:由于这是一个 php_flag 设置,如果您有 PHP 的自定义安装,您当然也可以在 php.ini 中默认设置它:)
回答by axew3
The question is
问题是
How to prevent Warning: POST Content-Length
如何防止警告:POST Content-Length
the answer is, without edit any config value, put this code at the very start of the file that will receive the form to be processed:
答案是,在不编辑任何配置值的情况下,将此代码放在将接收要处理的表单的文件的开头:
<?php // here start your php file
ob_get_contents();
ob_end_clean();
That is. No more warning, as well anything else you do not want as output on your page. After, as suggested elsewhere you can decide what to do with:
那是。不再有警告,以及您不希望作为页面输出的任何其他内容。之后,按照其他地方的建议,您可以决定如何处理:
if($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) && empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0){ echo"WoW your file is too big!"; }
Maybe also chunk the file, if in another way you pass correct values about file to be processed by Php.
如果以另一种方式传递有关要由 Php 处理的文件的正确值,也可以将文件分块。
as explained here Warning: POST Content-Lenght ...
回答by Romain GALVEZ
Here is the REAL solution to prevent this error properly.
这是正确防止此错误的真正解决方案。
Set this parameters in your php.ini :
在您的 php.ini 中设置此参数:
enable_post_data_reading = On
upload_max_size=0
post_max_size=0
Warning : be sure to have this line in your form
警告:一定要在你的表格中有这一行
<input type="hidden" name="MAX_FILE_SIZE" value="2048576" />
AND the MOST IMPORTANT :
最重要的是:
be sure to check the code errors of $_FILES['file']['error']
be sure to check size of your upload in PHP too
一定要检查$_FILES['file']['error']的代码错误
一定要检查你上传的 PHP 大小
回答by sibaz
I've got a similar problem to you, and wondered if you'd found a satisfactory solution. Currently, my 'PHP Warning: POST Content-Length of 21010354 bytes exceeds the limit of 8388608' is in the log, and I don't think there is much I can do about it.
我遇到了与您类似的问题,想知道您是否找到了令人满意的解决方案。目前,我的“PHP 警告:21010354 字节的 POST 内容长度超过 8388608 的限制”在日志中,我认为我对此无能为力。
I've working within a Zend framework, and when a user uploads a file as above, it basically kills all the usual POST data parsing, so the response to getParams() contains none of the usual POST parameters. This means the users is getting a validation error, saying one of the fields is missing, which is misleading.
我在 Zend 框架中工作,当用户上传文件时,它基本上会终止所有常见的 POST 数据解析,因此对 getParams() 的响应不包含任何常见的 POST 参数。这意味着用户收到验证错误,说其中一个字段丢失,这是一种误导。
In researching this, I've found a few interesting php_ini settings you might want to look at, notably enable_post_data_reading and always_populate_raw_post_data. In my case, I'm uploading a 20M file, with a 8M post max and a 10M uploaded file max, and the result from php://input seems to be a full on 20M file, even though the post data is now empty.
在研究这一点时,我发现了一些您可能想要查看的有趣的 php_ini 设置,特别是 enable_post_data_reading 和 always_populate_raw_post_data。在我的例子中,我上传了一个 20M 的文件,最大发布 8M,最大上传文件 10M,来自 php://input 的结果似乎是一个完整的 20M 文件,即使发布数据现在是空的.
So I'm going to manually check if the raw data exceeds the limit, and assume everything failed if it did (and bail). I suggest you could write your own code to parse the raw data, to populate the $_POST data. Then you can startup your code, as you wish, and only when you're ready, check if the user has uploaded more data than the limit. It's down to you, to then return an error before parsing it, or if it's ok, to parse it. As long as you set the PHP post and upload limits, to above your user limit, it should allow the user to break the rules, allowing your code to detect the breech and complain about it.
所以我将手动检查原始数据是否超过限制,并假设一切都失败了(并保释)。我建议您可以编写自己的代码来解析原始数据,以填充 $_POST 数据。然后你可以按照你的意愿启动你的代码,只有当你准备好时,检查用户上传的数据是否超过了限制。这取决于你,然后在解析它之前返回一个错误,或者如果可以,解析它。只要您将 PHP 发布和上传限制设置为高于您的用户限制,它就应该允许用户违反规则,让您的代码检测到漏洞并进行投诉。
回答by Alexander
As you have already found out for yourself, it is impossible to do it in PHP, since PHP can only catch errors from line 1 onwards.
正如您自己已经发现的那样,在 PHP 中是不可能做到的,因为 PHP 只能从第 1 行开始捕获错误。
The only way around would be to dig in the PHP source code, find where that error is thrown and remove that part from PHP sources before compiling your costume php version.
唯一的解决方法是挖掘 PHP 源代码,找到该错误发生的位置,并在编译您的服装 php 版本之前从 PHP 源中删除该部分。
But be warned that the error message is thrown for a reason.
但请注意,抛出错误消息是有原因的。
回答by Martin
To Prevent this error to happen you have to check the size of the uploaded files before and in case they're to big you have to cancel the upload!
为防止发生此错误,您必须先检查上传文件的大小,如果它们太大,您必须取消上传!
To just not show them i think it should be enough if you write in your php script
为了不显示它们,我认为如果您在 php 脚本中编写就足够了
error_reporting(0);
回答by CS GO
1.You can check the file size before uploading in JavaScript but this can be tampered !
1.上传前可以在JavaScript中检查文件大小,但这可以被篡改!
2.Check the file size after uploaded and beforesaving the file.
2.上传后和保存文件前检查文件大小。
<?php
$size = filesize($uploaded_file);
if($size > $your_size_limit)
{
echo "Throw bunch of errors";
}
else
{
//save it
}
?>
3.Don't try to hide the errors to solve the problem.
3.不要试图隐藏错误来解决问题。

