Excluding non-JPEG images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Excluding non-JPEG images

Post 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.";
}
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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;
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Try getimagesize.
If it doesn't return an array or element[2] is not 2 it's not a jpeg image.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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().
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

volka wrote:If it doesn't return an array or element[2] is not 2 it's not a jpeg image.
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post 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;
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or don't use list() at all for this ;)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.)
Post Reply