Jump to content

Archived

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

onlyican

checking is jpg is really a jpeg

Recommended Posts

Hello

I am creating a register form
On the form, I allow a user to upload an image
The image can be either
jpg, jpeg, gif, png

I am checking the file extension for this

BUT
if somoene has a BITMAP and they manually change the file extension to jpg
then when it comes to resizing the image, the code will mess up
Example
if they changed a BITMAP to .jpg
then when it reaches a line of code line

imagecreatefromjpeg()
it will thow an error

I know I can stop this by using
@imagecreatefromjpeg
BUT

is there a way to check the actual type of image they are uploading without using the file extension

Share this post


Link to post
Share on other sites
Not sure, never used it, but you could try this: http://www.php.net/manual/en/function.image-type-to-extension.php

Share this post


Link to post
Share on other sites
getimagesize() is easy enough. it returns a variable indicating the real file type, regardless of what the extension is.

Share this post


Link to post
Share on other sites
you could also try this
[code]
<?php
if (exif_imagetype('image.jpeg') != IMAGETYPE_JPEG) {
  echo 'The picture is not a jpeg';
}
?>
[/code]

information taken from <a href="http://uk.php.net/manual/en/function.exif-imagetype.php"> Here </a>

Share this post


Link to post
Share on other sites
if you're wanting to run a check on upload, just check the [i]type[/i] of the file in the $_FILES array. if it's image/jpeg, you should be good. keep in mind that this key of the array returns the MIME type of the file, so you've got to know what you're looking for first ;-)

Share this post


Link to post
Share on other sites
Thanks for your replies

This is for an upload
[code]
<?php
//First I am checking the file extension

$file_ext = substr($_FILES['ufile']['name'], strrpos($_FILES['ufile']['name'], '.')+1);

//then I am using getimagesize() to work it out

list($width, $height, $type) = getimagesize($_FILES["ufile"]["tmp_name"]);

//The type returns the ID number
//so using this array, it works out the file type

  $types = array(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');

//then the file extension is

$real_file_type = $types[$type];
?>
[/code]
This should help anyone else who wants this

Which are the only images I am allowing to upload

Thanks for all your help

Share this post


Link to post
Share on other sites

×

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.