如何检测用户上传的文件是否大于 post_max_size?

How to detect if a user uploaded a file larger than post_max_size?(如何检测用户上传的文件是否大于 post_max_size?)
本文介绍了如何检测用户上传的文件是否大于 post_max_size?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我应该如何以理智的方式处理超过 post_max_size 的 http 上传?

How should I go about handling http uploads that exceeds the post_max_size in a sane manner?

在我的配置中 post_max_sizeupload_max_filesize 大几 MB我遇到的问题是:
如果用户上传的文件超过 post_max_size

In my configuration post_max_size is a few MB larger than upload_max_filesize The problems I'm having are:
If a user uploads a file exceeding post_max_size

  • _POST 数组为空
  • _FILES 数组为空,当然其中不存在任何错误代码.
  • 没有其他信息可以通过这些方式访问什么样的表单帖子.

部分问题在于接收脚本根据 POST 的内容采取不同的操作.

Part of the problem is that the receiving script takes different actions depending on the contents of the POST.

我确实可以访问 _SERVER 变量并且可以获得关于发生了什么的线索,即 CONTENT_TYPECONTENT_LENGTHREQUEST_METHOD.然而,根据这些内容进行猜测似乎很成问题.

I do have access to the _SERVER variables and can get clues as to what happened, i.e. CONTENT_TYPE, CONTENT_LENGTH and REQUEST_METHOD. It does however seem very problematic to make guesses based on those contents.

MEMORY_LIMIT(设置为相关大小的 10 倍)和 Apaches LimitRequestBody(设置为无限制)没有问题.

MEMORY_LIMIT (set to 10 times the relevant sizes) and Apaches LimitRequestBody (set to unlimited) are found to not be at fault.

就目前而言,我什至很难向用户提供任何有意义的信息.

As it stands now I have a hard time even providing any meaningful messages to the user.

有什么办法可以保留一些表单数据,以便更好地了解发生了什么问题?我非常不愿意离开 php.

Is there any way to retain some form data to get better clues as to what has gone wrong? I'm very reluctant to move away from php.

推荐答案

对于不需要服务器端更改的简单修复,我将使用 HTML5 文件 API 在上传之前检查文件的大小.如果超过已知限制,则取消上传.我相信这样的事情会奏效:

For a simple fix that would require no server side changes, I would use the HTML5 File API to check the size of the file before uploading. If it exceeds the known limit, then cancel the upload. I believe something like this would work:

function on_submit()
{
  if (document.getElementById("upload").files[0].size > 666)
  {
    alert("File is too big.");
    return false;
  }

  return true;
}

<form onsubmit="return on_submit()">
<input id="upload" type="file" />
</form>

显然这只是一个例子的骨架,并不是每个浏览器都支持这个.但是使用它不会有什么坏处,因为它可以以这样一种方式实现,它可以优雅地降级为旧浏览器的任何东西.

Obviously it's just a skeleton of an example, and not every browser supports this. But it wouldn't hurt to use this, as it could be implemented in such a way that it gracefully degrades into nothing for older browsers.

当然,这并不能解决问题,但它至少可以让您的一些用户以最少的努力感到满意.(他们甚至不必等待上传失败.)

Of course this doesn't solve the issue, but it will at least keep a number of your users happy with minimal effort required. (And they won't even have to wait for the upload to fail.)

--

顺便说一句,检查 $_SERVER['CONTENT_LENGTH'] 与帖子和文件数据的大小可能有助于检测是否有问题.我认为当出现错误时它不会为零,而 $_POST$_FILES 都是空的.

As an aside, checking $_SERVER['CONTENT_LENGTH'] vs the size of the post and file data might help detect if something failed. I think it when there is an error it will be non zero, while the $_POST and $_FILES would both be empty.

这篇关于如何检测用户上传的文件是否大于 post_max_size?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)