如何检测用户上传的文件是否大于post_max_size? [英] How to detect if a user uploaded a file larger than post_max_size?

查看:181
本文介绍了如何检测用户上传的文件是否大于post_max_size?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何处理超过 post_max_size 的健全的http上传?



配置 post_max_size 是一个大于 upload_max_filesize
的MB大小我遇到的问题是:

如果用户上传的文件超过了 post_max_size




  • _POST数组是空的

  • _FILES数组是空的,当然其中的任何错误代码都不存在。




  • 没有其他信息可以通过这些方式访问它。部分问题在于接收脚本根据POST的内容采取不同的操作。

    我可以访问 _SERVER 变量,并可以得到有关发生的线索,例如 CONTENT_TYPE CONTENT_LENGTH REQUEST_METHOD 。然而,根据这些内容进行猜测似乎是很有问题的。



    现在,我甚至很难给用户提供任何有意义的信息。

    有什么方法可以保留一些表单数据来获得更好的线索,我非常不愿意离开PHP。

    解决方案

    对于不需要更改服务器端的简单修复,我将使用HTML5 File API来检查上传前的文件。如果超过了已知限制,则取消上传。我相信类似这样的工作:
    $ b $ pre $函数on_submit()
    {
    if(document.getElementById (upload)。files [0] .size> 666)
    {
    alert(File is too big。);
    返回false;
    }

    return true;
    }

    < form onsubmit =return on_submit()>
    < input id =uploadtype =file/>
    < / form>

    显然这只是一个例子的骨架,并不是每个浏览器都支持这个。但是这样做不会有什么坏处,因为它可以以这样一种方式实现,即对于旧版本的浏览器,它可以优雅地降级为无。

    当然,这不是解决这个问题,但是至少可以让你的用户满意,而且只需要很少的努力。 (他们甚至不必等待上传失败。)

    -



    另外,检查 $ _ SERVER ['CONTENT_LENGTH'] 与帖子和文件数据的大小可能有助于检测是否失败。我认为当出现错误时它将不为零,而 $ _ POST $ _ FILES 都将是空的。


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

    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

    • The _POST array is empty
    • The _FILES array is empty, and of course any error codes therein are not present.
    • No other info what kind of form post it is is accessible through theses means.

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

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

    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.

    解决方案

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

    --

    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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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