Page 1 of 1

How to limit size of uploaded files?

Posted: Wed Jun 18, 2008 12:14 pm
by paulwk
I'm a newbie to PHP and bought a buggy PHP script, which I've fixed for the most part, but I'm having a difficult time limiting the upload image size on the upload page. I've tried to add code but keeping getting errors. Here's the code I need to limit image size uploads on:

Code: Select all

if(isset($_POST[s1]))
{
    if(!empty($_FILES[images][name][0]))
    {
        while(list($key,$value) = each($_FILES[images][name]))
        {
            if(!empty($value))
            {
                $NewImageName = $t."_offer_".$value;
                copy($_FILES[images][tmp_name][$key], "cars_images/".$NewImageName);
 
                $MyImages[] = $NewImageName;
            }
        }
 
        if(!empty($MyImages))
        {
            $ImageStr = implode("|", $MyImages);
        }
 
    }
Can anyone suggest a solution?

Thanks!

Re: Quick Help

Posted: Wed Jun 18, 2008 12:25 pm
by WebbieDave
You can get the size of the uploaded file from $_FILES['userfile']['size']

For more info, check out:

http://us.php.net/features.file-upload

Re: Quick Help

Posted: Wed Jun 18, 2008 1:37 pm
by paulwk
Thanks, I've tried that and I keep getting back the error about modifying header information.

Re: Quick Help

Posted: Wed Jun 18, 2008 1:56 pm
by Christopher
Use a function like filesize() to check the size of $_FILES[images][tmp_name]. Only call move_uploaded_file() if the file size is below your limit.

Re: Quick Help

Posted: Wed Jun 18, 2008 1:58 pm
by califdon
1. Please don't use vague Subjects like "Quick Help." Most of the regular contributors simply don't read topics with such Subjects, so you are automatically excluding many of the most helpful people from helping you.

2. Post your exact error messages. We can't help you without knowing what is happening.

Re: Quick Help

Posted: Wed Jun 18, 2008 7:41 pm
by LSJason
You can use something like this in the HTML upload form:

Code: Select all

<input type="hidden" name="MAX_FILE_SIZE" value="512000" />

Re: Quick Help

Posted: Thu Jun 19, 2008 9:22 am
by paulwk
Thanks Jason, I think that will do the trick.

Re: Quick Help

Posted: Thu Jun 19, 2008 9:25 am
by paulwk
califdon thanks for the advice. I'm not new to forums, just this one. I should have known better then to post quick help, but I knew this was going to be an easy fix for most php developers. I myself have zero to very little php experience and I kind of figured out a solution.