How to detect if a user uploaded a file larger than post_max_size?

后端 未结 6 1379
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 12:00

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

6条回答
  •  生来不讨喜
    2020-12-06 12:09

    I liked @Matthew answer, but needed a version that checked for multiple upload files.

    This was my solution:

    function checkAttachmentsSize() {
        var total = 0;
        var count = 0;
    
        jQuery('input[type="file"]').each(
            function() {
                if (typeof this.files[0] != 'undefined') {
                    total+= this.files[0].size;
                    count++;
                }
            }   
        );
    
        var word = (count > 1) ? 's are' : ' is';
        if (total > (uploadMax * 1000 * 1000)) {
            alert("The attachment file" + word + " too large to upload.");
            return false;
        }
    
        return true;
    }
    

    And, for completeness, here's the binding of the function to the form being submitted:

    jQuery(function($) {
        $("form").submit(
            function() {
                return checkAttachmentsSize();
            }   
        });
    );
    

    NOTE:
    uploadMax is a variable that I set via php after calculating the maximum size of the allowable upload.

提交回复
热议问题