简体   繁体   中英

PHP Warning: POST Content-Length exceeds the limit

I have a simple contact form with file upload. I got the following warning, but I would like to make an error message for the users that this file is bigger than the limit. Yes I know i can disable the "Warning: POST Content-Length..." message if I switch off error_reporting, but I would warn the users. One more problem appeared when I fill the contact form and try to upload bigger file I press submit and all my filled fileds will be empty. Is it any kind of way to get the error message and don't lose the filled fields?

You could do that in Javascript first so that even before the File could hit the Server, Javascript would alert/inform the User that the file Size is more than the Maximum Allowed Size. Here below is an example, which you may well test out and fiddle with first here .

    <html>
    <head></head>
    <body>
    <input type="file" id="file_upload_1" name='file_upload_1' class="file_upload"/>
    <!-- AND THEN, OTHER INPUT FORM FIELDS FOLLOW -->

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        (function($) {
            $(document).ready(function(){
                var fileInput       = $("#file_upload_1");
                var MaxAllowedSize  = 2000; // 2KB
                var sizeKMGB        = "2KB";

                fileInput.on("change", function(evt){
                    var objFile     = $(this);
                    var fileSize    = document.getElementById("file_upload_1").files[0].size;
                    console.log(fileSize);
                    if(fileSize > MaxAllowedSize){
                        // RESET THE UPLOAD TO NOTHING... SO USER CAN UPLOAD AGAIN...
                        var fSize   =  parseFloat(fileSize/1000).toFixed(2);
                        var sUnit   = fSize + "KB";
                        if(fSize >= 100){
                            fSize   = parseFloat( fileSize/(1000*1000)).toFixed(2);
                            sUnit   = fSize + "MB";
                        }
                        objFile.val(null);
                        alert("Upload File-size Must not exceed " + sizeKMGB + ". \nYou tried to upload a File with about " + sUnit + ".")
                    }
                });
            });
        })(jQuery);
    </script>

    </body>
    </html>

What I've done is built an ajax script to check the maximum file size. Thus we can warn users before they upload a large file. Much nicer experience than having the upload fail.

First, the javascript (the example uses jQuery, but that's by no means necessary):

var max_size = // Insert ajax code to fetch max size *in bytes* from server here.
$('form input[type="file"]').each(function() {
    if ((typeof this.files != 'undefined') && this.files.length) {
        total_size += this.files[0].size;
    }
});
if (max_size < total_size) {
    var warning = 'Maximum file size is ' + max_size + ' bytes';
    return;
}

On the server side, the maximum upload size is determined by the smallest of these three values:

function max_size() {
    return Size::smallest(
        Size::str_to_bytes(ini_get('post_max_size')),
        Size::str_to_bytes(ini_get('memory_limit')),
        Size::str_to_bytes(ini_get('upload_max_filesize'))
    );
}

Writing the str_to_bytes and smallest functions is left as an exercise for the reader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM