Checking filesize?

Hello All,

Can anyone please help me. I’m trying to create a form which checks that a file is not over 39kb.


if ($_FILES['ufile']['size'][0] > 39000){
//Also tried if ($_FILES['ufile']['size'] > 39000){
echo "Sorry this file is over 39kb";
}
<input name="ufile[]" type="file" id="ufile"  tabindex="6"/>

However, it just seems to ignore this rule?

Can anyone please advise?

Thanks

To debug throw this code in:


var_dump($_FILES);

I reckon the problem will be using <input name=“ufile” …>
If you need multiple upload slots the size will probably be at:


$_FILES['ufile'][0]['size']

Thanks. Got it checking for the error now thanks to var_dump, however it seems to be erroring a message even if it’s less than 39kb:


if ($_FILES['ufile']['size'] > 39000){
echo "problem";
}


["size"]=> array(1) { [0]=> int(3365)

If you are still naming your input field as ufile then $_FILES[‘ufile’][‘size’] will be an array. You can see that from the second bit of code you posted (the var_dump)


["size"]=> array(1) { [0]=> int(3365)

You need to check $_FILES[‘ufile’][size’][0], and if you need the to create multiple upload fields you’ll need to do this in a loop so you can check all of the uploads, not just the first one.

If you DON’T need multiple uploads get rid of the in your HTML.

Btw, 39kb is 39936 bytes.

Yep, 39 * 1024, not 39 * 1000 :slight_smile: