Jump to content


Photo

Restrict file size on image upload


  • Please log in to reply
1 reply to this topic

#1 ivalea

ivalea
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 11 March 2006 - 12:40 AM

I've got a page where user can upload images and I'm wondering how to restrict file size by pixels and echo error if too large. My code is this:


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], "re_images/".$NewImageName);

                $MyImages[] = $NewImageName;
            }
        }

        if(!empty($MyImages))
        {
            $ImageStr = implode("|", $MyImages);
        }
        

    }

Other possibility - any way to check file size through javascript?

Thanks!


#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 11 March 2006 - 01:14 AM

Javascript is "sandboxed" and has no interaction with your local computer's file system execpt through cookies, so there is no way to check with js.

Use the getimagesize function:

list($width, $height, $type, $attr) = getimagesize("img.jpg");
$pixels = $width * $height;
if ($pixels > $maxpixels) {
  do something
} else {
  do something else
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users