1. <tfoot id='VzSO0'></tfoot>
      <bdo id='VzSO0'></bdo><ul id='VzSO0'></ul>
    <legend id='VzSO0'><style id='VzSO0'><dir id='VzSO0'><q id='VzSO0'></q></dir></style></legend>

    1. <i id='VzSO0'><tr id='VzSO0'><dt id='VzSO0'><q id='VzSO0'><span id='VzSO0'><b id='VzSO0'><form id='VzSO0'><ins id='VzSO0'></ins><ul id='VzSO0'></ul><sub id='VzSO0'></sub></form><legend id='VzSO0'></legend><bdo id='VzSO0'><pre id='VzSO0'><center id='VzSO0'></center></pre></bdo></b><th id='VzSO0'></th></span></q></dt></tr></i><div id='VzSO0'><tfoot id='VzSO0'></tfoot><dl id='VzSO0'><fieldset id='VzSO0'></fieldset></dl></div>

      <small id='VzSO0'></small><noframes id='VzSO0'>

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

      时间:2023-05-21

          <small id='sWzyr'></small><noframes id='sWzyr'>

            <bdo id='sWzyr'></bdo><ul id='sWzyr'></ul>
            <legend id='sWzyr'><style id='sWzyr'><dir id='sWzyr'><q id='sWzyr'></q></dir></style></legend>
              <tbody id='sWzyr'></tbody>

            • <tfoot id='sWzyr'></tfoot>
                <i id='sWzyr'><tr id='sWzyr'><dt id='sWzyr'><q id='sWzyr'><span id='sWzyr'><b id='sWzyr'><form id='sWzyr'><ins id='sWzyr'></ins><ul id='sWzyr'></ul><sub id='sWzyr'></sub></form><legend id='sWzyr'></legend><bdo id='sWzyr'><pre id='sWzyr'><center id='sWzyr'></center></pre></bdo></b><th id='sWzyr'></th></span></q></dt></tr></i><div id='sWzyr'><tfoot id='sWzyr'></tfoot><dl id='sWzyr'><fieldset id='sWzyr'></fieldset></dl></div>
              • 本文介绍了如何检测用户上传的文件是否大于 post_max_size?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我应该如何以理智的方式处理超过 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    <legend id='WdJpf'><style id='WdJpf'><dir id='WdJpf'><q id='WdJpf'></q></dir></style></legend>
                  1. <i id='WdJpf'><tr id='WdJpf'><dt id='WdJpf'><q id='WdJpf'><span id='WdJpf'><b id='WdJpf'><form id='WdJpf'><ins id='WdJpf'></ins><ul id='WdJpf'></ul><sub id='WdJpf'></sub></form><legend id='WdJpf'></legend><bdo id='WdJpf'><pre id='WdJpf'><center id='WdJpf'></center></pre></bdo></b><th id='WdJpf'></th></span></q></dt></tr></i><div id='WdJpf'><tfoot id='WdJpf'></tfoot><dl id='WdJpf'><fieldset id='WdJpf'></fieldset></dl></div>
                  2. <small id='WdJpf'></small><noframes id='WdJpf'>

                      • <bdo id='WdJpf'></bdo><ul id='WdJpf'></ul>

                          <tbody id='WdJpf'></tbody>

                      • <tfoot id='WdJpf'></tfoot>