PHP - 当上传大文件时,清空$ _POST和$ _FILES [英] PHP - empty $_POST and $_FILES - when uploading larger files

查看:760
本文介绍了PHP - 当上传大文件时,清空$ _POST和$ _FILES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有这样几个问题(空$ _POST,$ _FILES),但我没有找到任何解决方案或解释。



HTML表单:

 < form enctype =multipart / form-datamethod =post行动= upload.php的 > 
< p>
< input type =hiddenname =MAX_FILE_SIZEvalue =10000000/>
< input type =filename =image/>
< / p>
< p>
< input type =textname =other_field/>
< / p>
< / form>

upload.php

 的print_r($ _ POST); // array()
print_r($ _ FILES); // array()
exit;

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



PHP网站更新(解释/解决方案)



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



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

解决方案

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



选项a)

<$ p $('_ code> if(intval($ _ SERVER ['CONTENT_LENGTH'])> 0& count($ _ POST)=== 0){
throw new Exception('PHP discarded POST数据由于请求超过post_max_size。');
}

选项b)

添加一个GET参数,是否存在POST数据。


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).

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

HTML form:

<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

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.

Updated (explanation/solution) from PHP site

From PHP site (I missed this section): 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.

解决方案

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.

Option 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.

这篇关于PHP - 当上传大文件时,清空$ _POST和$ _FILES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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