0

Possible Duplicate:
Client Checking file size using HTML5?

How can I inform the server about the file size, before the file is being transmitted? With HTML/HTML5, JS and PHP (without Flash)?

I want to prevent it, that large files are uploaded and this before the upload process; is it possible?

thank you very much and sorry for my german-english..

eb

Community
  • 1
  • 1
Emil
  • 1
  • 1
  • 1

5 Answers5

1
if (typeof FileReader !== "undefined") {
var size = document.getElementById('myfile').files[0].size;
// check file size
}  
ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
1

For client side take a look at File API it's html5 feature though some, browsers won't work. compatibility list

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
0
$(function(){

      $('#submit_button_id').click(function(){

            var f=this.files[0]
            if(f.size > 10000 || f.fileSize > 10000)
            {
               return false;
            }
        });
    });
asprin
  • 9,579
  • 12
  • 66
  • 119
0

Have a look at the PHP filesize function.

Also, you can use $_FILES["fieldname"]["size"] and then check:

if($_FILES["fieldname"]["size"]>150000){ echo "too large"; exit;}
Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
  • 3
    This is not before the upload process though as requested. They might have already waited an age to upload a large file before you reject it which I assume is what the OP wants to prevent. – Chris Jul 24 '12 at 09:13
  • Good point. The server will not be notified about the file size if it is done client side, though, so server side validation will have to be done anyways. Maybe this snippet helps for that :) – Dennis Hackethal Jul 24 '12 at 09:33
-1

Think the only way to realize your task is to validate via JS before submit. Take a look at this Validation by JS

donald123
  • 5,638
  • 3
  • 26
  • 23