Detecting file upload size on the client side? Detecting file upload size on the client side? php php

Detecting file upload size on the client side?


On MAX_FILE_SIZE

Read This:

...At http://pk.php.net/manual/en/features.file-upload.post-method.php and equivalent locations in other formats, it is statedthat browsers take the value of a MAX_FILE_SIZE form field intoaccount.

This information is repeated elsewhere on the web and in books, butappears to originate from the PHP documentation (it does not appear interms of other server-side technologies).

There is nothing in any of the HTML, HTTP or related specs to indicatethat this is the case (in particular RFC 1867 which introduced fileuploads to HTML doesn't mention it, so it isn't even a case of a kludgethat was mentioned in the first RFC and then dropped) nor does it makesense in the context of the HTML specs (there is nothing to indicate anyrelationship between that particular hidden input and the file input).The only statements about hidden fields I could find in any of them waswarnings in the security considerations sections against user-agentsbasing any file-related operations on anything mentioned in a hiddenfield.

No browsers appear to perform this as an "extension". Indeed given thatthere are potentially other possible meanings for a hidden field withthat name in an application handling several file uploads, it would haveto be considered a design flaw any any did.

I submit that there is no such mechanism in mainstream browsers (if anyat all) and indeed shouldn't be. Reference to it should be dropped fromdocumentation.

I'd further suggest that since this idea has propagated from thisdocumentation elsewhere that a note about it not working should to beadded.

If a mechanism is required or desired for more rapidly handling thissort of file handling issue then it requires functionality to allow PHPto intercept streams being uploaded before request completion, whichwould be completely different to how this documentation suggest itshould be dealt with, even if it was true...


the code below come from swfUpload php implementation:

// Check post_max_size (http://us3.php.net/manual/en/features.file-upload.php#73762)    $POST_MAX_SIZE = ini_get('post_max_size');    $unit = strtoupper(substr($POST_MAX_SIZE, -1));    $multiplier = ($unit == 'M' ? 1048576 : ($unit == 'K' ? 1024 : ($unit == 'G' ? 1073741824 : 1)));    if ((int)$_SERVER['CONTENT_LENGTH'] > $multiplier*(int)$POST_MAX_SIZE && $POST_MAX_SIZE) {        header("HTTP/1.1 500 Internal Server Error");        echo "POST exceeded maximum allowed size.";        exit(0);    }// Validate the file size (Warning the largest files supported by this code is 2GB)    $max_file_size_in_bytes = 2147483647;               $file_size = @filesize($_FILES[$upload_name]["tmp_name"]);        if (!$file_size || $file_size > $max_file_size_in_bytes) {            HandleError("File exceeds the maximum allowed size");            exit(0);        }


This probably only works on Firefox 3.6 for now:

<script type="text/javascript">    function checkSize()    {        var input = document.getElementById("upload");        // check for browser support (may need to be modified)        if(input.files && input.files.length == 1)        {                       if (input.files[0].fileSize > 1024) /* or maybe .size */            {                alert("The file must be less than 1KB");                return false;            }        }        return true;    }</script><form method="post" enctype="multipart/form-data" onsubmit="return checkSize()">        <input type="file" id="upload" />    <input type="submit" /></form>

See http://www.w3.org/TR/FileAPI/.


As far as I know there is no simple, cross-browser solution to achieve this. The only working solutions are Flash or Java based since these technologies can access filesystem and get file info.

Example scripts: YUI2 Uploader, FancyUpload, SWFUpload