0

I want to have a small upload limit (e.g. 100 kb for testing, perhaps 6 Mb ultimately). The size of the upload can be checked:

  1. before the upload

  2. fail when too much is uploaded

  3. after the upload

If the user is trying a 1 Gb file ideally (1) should happen so that the file isn't uploaded at all. If not, (2) should happen so that it doesn't take long before the user knows the file is too big. I'd like to avoid the other possibility (3).

In HTML5 but not in IE9 the filesize can be checked before uploading using:

this.files[0].size

Get file size before uploading

In IE9 the following might work if the security settings are adjusted:

var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb

Ideally I'd like to use a method that works with IE9. I've heard about flash files being used. I'd like a method that is separate - not using a big plugin.

Here is my code. At the moment it uploads the whole file before it checks if the file size is too big.

<?php
if (isset($_FILES['myfile'])) {
    if ($_FILES['myfile']['error'] == UPLOAD_ERR_FORM_SIZE) {
        // $_FILES['myfile']['size'] is 0
        echo 'Error: file is too big!<br>';
    }
    else if ($_FILES['myfile']['size'] > 100000) {
        echo 'File size is too big!<br>';
    }
    else {
        echo 'File uploaded ok.<br>';
    }
}
var_dump($_FILES);
?>
<form method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <input type="file" name="myfile" />
    <input type="submit" value="Submit" />
</form>

The small upload limit is just for one form so I don't want to change the global PHP settings file.

I'm not sure using MAX_FILE_SIZE is a good idea - the person still has to upload the entire file and the filesize data is lost (I might want to tell people how big their upload was)

ini_get('post_max_size') returns 8M and ini_get('upload_max_filesize') returns 32M. If files that are over 8 Mb try to be uploaded the file is still fully uploaded (even 40+ Mb). After larger than 8 Mb files are uploaded var_dump($_FILES) returns an empty array.

If the file is bigger than about 600 Mb I get a 413 error... that is (1).

So I want to do (1) or (2) in IE9, PHP and jQuery.

Community
  • 1
  • 1
Luke Wenke
  • 1,149
  • 2
  • 23
  • 43

0 Answers0