gpt4 book ai didi

php - 如何优雅地处理超过 PHP 的 `post_max_size` 的文件?

转载 作者:搜寻专家 更新时间:2023-10-31 22:07:16 26 4
gpt4 key购买 nike

我正在开发一个将文件附加到电子邮件的 PHP 表单,并试图优雅地处理上传文件过大的情况。

我了解到 php.ini 中有两个设置会影响文件上传的最大大小:upload_max_filesizepost_max_size .

如果文件大小超过upload_max_filesize,PHP 返回文件大小为0。没关系;我可以检查一下。

但如果它超过 post_max_size,我的脚本会自动失败并返回到空白表单。

有什么方法可以捕捉到这个错误吗?

最佳答案

来自 the documentation :

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. <form action="edit.php?processed=1">, and then checking if $_GET['processed'] is set.

很遗憾,看起来 PHP 并没有发送错误。由于它发送的是空的 $_POST 数组,这就是为什么您的脚本返回到空白表单的原因 - 它认为它不是 POST。 (相当糟糕的设计决定恕我直言)

This commenter还有一个有趣的想法。

It seems that a more elegant way is comparison between post_max_size and $_SERVER['CONTENT_LENGTH']. Please note that the latter includes not only size of uploaded file plus post data but also multipart sequences.

关于php - 如何优雅地处理超过 PHP 的 `post_max_size` 的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16680333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com