SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:
    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

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To debug throw this code in:
    PHP Code:
    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:
    PHP Code:
    $_FILES['ufile'][0]['size'

  3. #3
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    if ($_FILES['ufile']['size'] > 39000){
    echo "problem";
    }
    Code:
    ["size"]=> array(1) { [0]=> int(3365)

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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)
    Code:
    ["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.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Btw, 39kb is 39936 bytes.
    Yep, 39 * 1024, not 39 * 1000


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •