Validation failing using PHP's getimagesize function

I'm validating form input and I'm having trouble with the file upload control.  First, here are the conditions :

1 - image must be no larger than 50 x 50 pixels
2 - image must be of type .GIF, .JPG or .PNG

I've been looking at the getimagesize function and it seems to suit my needs just fine in this case.  The filesize validation works as it should but the second validation on type is letting any old file in.  Could be and executable, could be a text file...anything is being accepted and I'm not quite sure why.
Here's my code :
list($width, $height, $type, $attr) = getimagesize($target_path);
if($width > 50 || $height > 50) { 
     $err_msg .= "\\nImage must be no larger than 50 x 50 pixels in size";
     $image_err = true;
     $errors = true;
}
if($type < 1 && $type > 3)  {
     $err_msg .= "\\nImage must be of type .GIF, .JPG or .PNG";
     $image_err = true;
     $errors = true;
}
if($image_err == true)  { unlink($target_path);  }

Open in new window


According to Jesus Zamora's comment on the PHP reference site, type 1 should be .GIF, 2 should .JPG and 3 should be .PNG.  Here's a link to the page :

http://php.net/manual/en/function.getimagesize.php

Any help in the matter would be much appreciated.

duder78Asked:
Who is Participating?
 
maosalahCommented:
in upload for try

if ($_FILES["imagefile"]["type"] == "image/jpeg" || $_FILES["imagefile"]["type"] == "image/gif" || $_FILES["imagefile"]["type"] == "image/png") {  }

good luck
0
 
Michael701Commented:
Without looking up the command I question your logic


if($type < 1 && $type > 3)  {


should this be an 'OR' statement


if($type < 1 || $type > 3)  {
0
 
Michael701Commented:
After looking up the command, YES

if($type < 1 || $type > 3)  {
0
 
duder78Author Commented:
My bad, it was supposed to be ||.  Doesn't help matters though.  maosalah, i'm going to try your code and see what happens.

0
 
duder78Author Commented:
That's the ticket man.  I'll just use getimagesize to validate the image size then.  Thanks alot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.