determine image size while uploading

Discussion in 'PHP' started by beermaker74, Mar 14, 2007.

  1. #1
    ok i have a script that uploads images. I have a script that checks the file size and if it is too big it doesnt upload it. But it seems to upload the whole image into memory b4 it determines if it too big. Is there any way to check the file sizes b4 it is uploaded into memory? I have 12 fields and I have the image size limit at 1.5mb. so if they tried to upload 12 1.6mb files it would take 10 minutes and then not do any uploading. I would like to loop thru the files and determine if they are too big b4 any of my resources are used up. Am i explaining this well?
    I am going to put a big warning on the page. But i would like to idiot proof it. thanks
     
    beermaker74, Mar 14, 2007 IP
  2. AndrewR

    AndrewR Peon

    Messages:
    108
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Actually, I think you meant the size in kb.

    $_FILES['name']['size'] will output the KB size of the image. Do basic math calculations to find MB or whatever you need.
     
    AndrewR, Mar 14, 2007 IP
  3. beermaker74

    beermaker74 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am using that to determine the file size in kb and i have a max file size that I compare it to. but as I was saying. It seems to upload the whole image b4 it determines if it is too big. I am testing onefile that is 1.6 mb and it takes about 20 seconds to continue with the script. I am looking for instant confirmation b4 it is uploaded into memory. Is this possible?
     
    beermaker74, Mar 14, 2007 IP
  4. AndrewR

    AndrewR Peon

    Messages:
    108
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You mean before it uploads a temporary image or final? The final upload is initiated by you using move_uploaded_file(), so it must be an error in your coding.
     
    AndrewR, Mar 14, 2007 IP
  5. stugs

    stugs Peon

    Messages:
    157
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you want to check the filesize before upload, you need to look at either some javascript or flash on the client entd. The best way to do this is probably to use a flash uploader as many clients ignore javascript, thus defeating your system.

    Check out swfupload.mammon.se. I don't have enough posts to post that as a link :)
     
    stugs, Mar 14, 2007 IP
  6. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #6
    To check image size before uploading, you can try this:

    $nPhotoSize = $_FILES['photo']['size']; // size of uploaded file
    if ($nPhotoSize == 0) // if there is no image selected
    {
    die ("Sorry. The upload of $sPhotoFileName has failed. Search a photo smaller than 100K.");
    }

    if ($nPhotoSize > 102400) // if image is larger than 100 kb
    {
    die ("Sorry. The file $sPhotoFileName is larger than 100K. Advice: reduce the photo using a drawing tool.");
    }
    else
    {
    <!-- paste image uploading code here-->
    }
     
    srobona, Mar 14, 2007 IP
  7. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $filesize = $_FILE['file']['size'];

    or

    <?php

    // outputs e.g. somefile.txt: 1024 bytes

    $filename = 'somefile.txt';
    echo $filename . ': ' . filesize($filename) . ' bytes';

    ?>
     
    jitesh, Mar 14, 2007 IP