1

So, we allow a few uploads to our web service.

Avatars and such.

We set the maximum size of these file uploads to 8MB via php's upload_max_filesize.

This works great, except that if a user uploads a larger image, for example 15MB, an error will be dumped in our php log.

We want to avoid this.

So:

Is there anyway to set via HTML the maximum upload size of a file so uploads over our specified size are not even sent to our server and thus, the error showing in the log is avoided?

Here is the PHP error we are trying to avoid BTW:

PHP Warning:  POST Content-Length of 11978117 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
anonymous-one
  • 14,454
  • 18
  • 60
  • 84
  • Hints here http://stackoverflow.com/questions/6327965/html-upload-max-file-size-does-not-appear-to-work – Mina Oct 27 '13 at 10:12
  • i should mention : i understand this is purely cosmetic... the error dumped in the php log is a warning... but i would think there should be a way to avoid having these errors show up in our log file... which we end up having to look at as soon as we see there is any content in there... – anonymous-one Oct 27 '13 at 10:13
  • Well, you can prevent PHP to log all Warnings, or temporary set the `error_reporting` to do so in a particular method. – Mina Oct 27 '13 at 10:15
  • kyra: a) the method shown in 6327965 works... except in ie :( which is a major drawback. b) since this warning is dumped prior to php actually parsing + executing the script, without doing some fancy apache setEnv etc etc stuff, there is no way to simply do a ini_set(...ignore errors...). plus that would end up hiding all other errors in our avatar upload handler... which we wouldnt want to do. – anonymous-one Oct 27 '13 at 10:19
  • @anonymous-one One can also upload files without using a browser, so you won't ever be able to hide *all* errors. Regarding IE, one way would be to use the JavaScript File API to check the file size. – ComFreek Oct 27 '13 at 10:24

1 Answers1

1

You cannot restrict the file size using HTML markup. (In very early days, RFC 1867 said that the maxlength attribute would do this, but this feature was never implemented or taken into HTML specs.)

However, reasonably modern browsers support the File API, which lets you code such checks using client-side JavaScript. Example:

<script>
function checkFiles(files) {
  var ok = true;
  if(files) {
    for (var i = 0; i < files.length; i++) {
      var file = files[i];
      if(file.size > 8 * 1024 * 1024) {
         alert("The file" + file.name + " is too big.");
         ok = false; } } }
  return ok;
}
</script>
[...]
<form action="..." method="post" onsubmit=
 "return checkFiles(document.getElementById('f').files)"
[...]
<div><label for="f">File(s):</label>
<input id="f" type="file" name="file" multiple></div>
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390