Page 1 of 1

Excluding non-JPEG images

Posted: Fri Aug 25, 2006 9:01 am
by Leao
Hi,

I need a PHP script that excludes images sent from a form that aren't JPEGs. I originally tried this PHP script:

Code: Select all

<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']);
$ok=1;

if ($uploaded_type != 'image/jpeg') {
{
echo "Sorry your file needs to be a JPEG image.";
$ok=0;
}

if ($ok==0)
{
echo "Sorry your file was not uploaded";
}

else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], 'images/title.jpg'))
{
echo "Your image has been uploaded.";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>
This script worked perfectly on most computers, but on those that hide file extensions it rejected even valid JPEG images. I tried the getimagesize() method below too as an alternative means of rejecting non JPEG files. It doesn't work either, can you help? Thanks – Leao

Code: Select all

<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']);
$ok=1;

list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['tmp_name']) ;

if ($ImportMimeType != 'image/jpeg')
{
echo "Sorry your file needs to be a JPEG image.";
$ok=0;
}

if ($ok==0)
{
echo "Sorry your file was not uploaded";
}

else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], 'images/title.jpg'))
{
echo "Your image has been uploaded.";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>

Posted: Fri Aug 25, 2006 9:08 am
by JayBird
you need to do this

Code: Select all

if(!getimagesize($_FILES['uploaded']['tmp_name']))
{
    echo "your file is not an image";
    $ok = 0;
}

Posted: Fri Aug 25, 2006 9:09 am
by volka
Try getimagesize.
If it doesn't return an array or element[2] is not 2 it's not a jpeg image.

Posted: Fri Aug 25, 2006 9:15 am
by onion2k
The two posters above me are correct.. but I just thought I'd point out:

Code: Select all

list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['tmp_name']) ;

if ($ImportMimeType != 'image/jpeg')
$ImageMimeType in the list() .. $ImportMimeType in the if().

Posted: Fri Aug 25, 2006 9:29 am
by Leao
JayBird wrote:you need to do this

Code: Select all

if(!getimagesize($_FILES['uploaded']['tmp_name']))
{
    echo "your file is not an image";
    $ok = 0;
}
This code works fine in terms of rejecting files that aren't images but how can I modify the code to reject images that aren't JPEGs, e.g. to reject GIF and TIFF files?

Thank you,

Leao

Posted: Fri Aug 25, 2006 9:30 am
by volka
volka wrote:If it doesn't return an array or element[2] is not 2 it's not a jpeg image.

Posted: Fri Aug 25, 2006 9:33 am
by Leao
volka wrote:If it doesn't return an array or element[2] is not 2 it's not a jpeg image.
Sorry, I don't understand, can you explain? Cheers

Posted: Fri Aug 25, 2006 9:35 am
by volka
getimagesize returns an array with informations about the image (of false if it's not an image at all).
The element with the index 2 is a number describing the image type.
http://de2.php.net/getimagesize wrote:Index 2 is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.

Posted: Fri Aug 25, 2006 9:50 am
by feyd
avoid using list() and getimagesize() on the same line. If getimagesize() errors out you'll get a warning. Capture the output, properly check it the use list() on the correct outputs.

Posted: Fri Aug 25, 2006 9:51 am
by Leao
Thanks, I tried this and... it worked!

Code: Select all

if ($ImportMimeType != 2)
{
echo "Sorry your file needs to be a JPEG image.";
$ok=0;
}

Posted: Fri Aug 25, 2006 9:51 am
by volka
or don't use list() at all for this ;)

Posted: Fri Aug 25, 2006 12:33 pm
by Ollie Saunders
I greatly prefer exif_imagetype personally. For one you can read code written using it and actually know what the hell is going on.

Posted: Fri Aug 25, 2006 5:15 pm
by feyd
ole wrote:I greatly prefer exif_imagetype personally. For one you can read code written using it and actually know what the hell is going on.
Hopefully the host didn't compile without it (or didn't include the extension.)