Jump to content

Restrict file size on image upload


ivalea

Recommended Posts

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:

[code]

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);
        }
        

    }
[/code]

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

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/4653-restrict-file-size-on-image-upload/
Share on other sites

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:

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.