How to limit size of uploaded files?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulwk
Forum Newbie
Posts: 4
Joined: Wed Jun 18, 2008 12:05 pm

How to limit size of uploaded files?

Post 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!
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Quick Help

Post 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
paulwk
Forum Newbie
Posts: 4
Joined: Wed Jun 18, 2008 12:05 pm

Re: Quick Help

Post by paulwk »

Thanks, I've tried that and I keep getting back the error about modifying header information.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Quick Help

Post 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.
(#10850)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Quick Help

Post 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.
LSJason
Forum Commoner
Posts: 45
Joined: Mon May 12, 2008 4:43 pm

Re: Quick Help

Post 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" />
paulwk
Forum Newbie
Posts: 4
Joined: Wed Jun 18, 2008 12:05 pm

Re: Quick Help

Post by paulwk »

Thanks Jason, I think that will do the trick.
paulwk
Forum Newbie
Posts: 4
Joined: Wed Jun 18, 2008 12:05 pm

Re: Quick Help

Post 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.
Post Reply