Forum Moderators: coopster

Message Too Old, No Replies

Image upload - verify file type help

         

adammc

12:43 am on Jun 12, 2007 (gmt 0)

10+ Year Member



Hi guys,

Im having trouble getting this to work, i want to validate that the uploaded image ($picture) is a gif or jpeg.

[php]
// Validate the image type. Should be jpeg, jpg, or gif.
$allowed = array ('image/gif', 'image/jpeg', 'image/jpg');
if (in_array($_FILES['picture']['type'], $allowed))
{
$errors['picture2'] = 'Please ensure the image is a JPEG or GIF.';
}
[/php]

Can anyone possibly help?

adammc

3:13 am on Jun 12, 2007 (gmt 0)

10+ Year Member



got it sorted using:

function file_extension($filename) {
return strtolower(end(explode(".", $filename)));}

$allowed = array('gif', 'jpg', 'jpeg', 'png');if (!in_array(file_extension($_FILES['Picture']['name']), $allowed) { // Filetype not allowed}

adammc

3:13 am on Jun 12, 2007 (gmt 0)

10+ Year Member



One last question...
Does anyone know a better routine to check the dimensions of an image?

This one wont validate true for a 120wide x 90high image?

// Get image width and height and mime type
$image = getimagesize("$picture");
$type = $image['mime'];
$width = $image[0];
$height = $image[1];

// Validate the pictures dimensions (length & width set in config file)
if(($image[0] > $max_width) ¦¦ ($image[1] >$max_height)) {
$errors['picturedimensions'] = "Your image must not exceed ". $max_height . " x ". $max_width .".";
}

adammc

3:49 am on Jun 12, 2007 (gmt 0)

10+ Year Member



i figured it out.. Silly mistake!
I had the width and height set wrongly