PHP - 空 $_POST 和 $_FILES - 上传较大文件时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7852910/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:30:05  来源:igfitidea点击:

PHP - empty $_POST and $_FILES - when uploading larger files

phphtml

提问by Dmitry F

I have a following problem, I have HTML form that uploads a file with some extra information. But it allows to upload files that only less then 10MB. But when user tries to upload something bigger, both $_POST and $_FILES array are empty (I expected that $_POST will have some values and $_FILES will have some values but will indicate that there is an upload error).

我有以下问题,我有 HTML 表单,可以上传包含一些额外信息的文件。但它允许上传小于 10MB 的文件。但是当用户尝试上传更大的东西时,$_POST 和 $_FILES 数组都是空的(我预计 $_POST 会有一些值,而 $_FILES 会有一些值,但会表明存在上传错误)。

There is a few questions (empty $_POST, $_FILES) like that, but I didn't find any solution, or explanation for it.

有一些类似的问题(空 $_POST、$_FILES),但我没有找到任何解决方案或解释。

HTML form:

HTML表单:

<form enctype="multipart/form-data" method="post" action="upload.php">
    <p>
        <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
        <input type="file" name="image"  />
    </p>
    <p>
        <input type="text" name="other_field" />
    </p>
</form>

upload.php

上传.php

print_r($_POST);  // array()
print_r($_FILES); // array()
exit;

It works fine, if file size is under 10MB (file size limit is 10MB), and I don't want to increase it, I just want to capture an error in PHP.

它工作正常,如果文件大小低于 10MB(文件大小限制为 10MB),并且我不想增加它,我只想捕获 PHP 中的错误。

Updated (explanation/solution) from PHP site

从 PHP 站点更新(说明/解决方案)

From PHP site (I missed this section): http://us.php.net/manual/en/ini.core.php#ini.post-max-size

来自 PHP 站点(我错过了本节):http: //us.php.net/manual/en/ini.core.php#ini.post-max-size

Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. If memory limit is enabled by your configure script, memory_limit also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size. When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. , and then checking if $_GET['processed'] is set.

设置允许的帖子数据的最大大小。此设置也会影响文件上传。要上传大文件,此值必须大于upload_max_filesize。如果您的配置脚本启用了内存限制,则 memory_limit 也会影响文件上传。一般来说,memory_limit 应该大于 post_max_size。使用整数时,该值以字节为单位。也可以使用本常见问题解答中所述的速记符号。如果 post 数据的大小大于 post_max_size,则 $_POST 和 $_FILES 超全局变量为空。这可以通过多种方式进行跟踪,例如通过将 $_GET 变量传递给处理数据的脚本,然后检查是否设置了 $_GET['processed']。

采纳答案by Augustus Kling

As noted in the edited question $_POST and $_FILES are empty when PHP silently discards data (happens when the actual data is bigger than post_max_size). Since HTTP header and $_GET remain intact those can be used to detect the discards.

如已编辑的问题中所述,当 PHP 静默丢弃数据时,$_POST 和 $_FILES 为空(当实际数据大于 post_max_size 时发生)。由于 HTTP 标头和 $_GET 保持不变,它们可用于检测丢弃。

Option a)

选项a)

if(intval($_SERVER['CONTENT_LENGTH'])>0 && count($_POST)===0){
    throw new Exception('PHP discarded POST data because of request exceeding post_max_size.');
}

Option b)
Add a GET parameter that tells whether POST data is present.

选项 b)
添加一个 GET 参数,告知是否存在 POST 数据。

回答by Crontab

Run phpinfo()and check to make sure your upload_max_filesizeand post_max_sizedirectives are large enough.

运行phpinfo()并检查以确保您的upload_max_filesizepost_max_size指令足够大。

http://us.php.net/manual/en/ini.core.php#ini.upload-max-filesize

http://us.php.net/manual/en/ini.core.php#ini.upload-max-filesize

http://us.php.net/manual/en/ini.core.php#ini.post-max-size

http://us.php.net/manual/en/ini.core.php#ini.post-max-size

回答by Ondra ?i?ka

There are some limits - both on client and server side.

在客户端和服务器端都有一些限制。

On client side, the MAX_FILE_SIZE field is not of much use, perhaps browser may take it as a hint; but rather browsers follow their configured limits.

在客户端,MAX_FILE_SIZE 字段没有多大用处,也许浏览器可能会以此作为提示;而是浏览器遵循其配置的限制。

On server side, check php.ini for:

在服务器端,检查 php.ini 以获取:

upload_max_filesize = 5M

post_max_size = 5M 

max_input_time = ...

Also check Apache's log for notes about dropped POST body or such.

还要检查 Apache 的日志以获取有关丢弃的 POST 正文等的注释。

回答by Mir hossein Mousavi

i can mention that if you process form like below

我可以提一下,如果您处理如下表格

if(isset($_POST['submit'])

if post_max_size is lower than posted data then the $_POST['submit'] will be empty

如果 post_max_size 小于发布的数据,则 $_POST['submit'] 将为空

回答by Marc B

You can check for upload errors with:

您可以使用以下命令检查上传错误:

if ($_FILES['images']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['images']['error']);
}

The error codes are defined here.

错误代码在此处定义。