Jump to content


Photo

checking is jpg is really a jpeg


  • Please log in to reply
5 replies to this topic

#1 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 05 September 2006 - 04:49 PM

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
Tell me the problem, I will try tell you the solution

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 September 2006 - 05:10 PM

Not sure, never used it, but you could try this: http://www.php.net/m...o-extension.php

Info: PHP Manual


#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 05 September 2006 - 05:24 PM

getimagesize() is easy enough. it returns a variable indicating the real file type, regardless of what the extension is.
Legend has it that reading the manual never killed anyone.
My site

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 05 September 2006 - 05:27 PM

you could also try this
<?php
if (exif_imagetype('image.jpeg') != IMAGETYPE_JPEG) {
   echo 'The picture is not a jpeg';
}
?>

information taken from <a href="http://uk.php.net/ma...imagetype.php"> Here </a>
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 05 September 2006 - 06:00 PM

if you're wanting to run a check on upload, just check the type 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 ;-)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 05 September 2006 - 06:17 PM

Thanks for your replies

This is for an upload
<?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];
?>
This should help anyone else who wants this

Which are the only images I am allowing to upload

Thanks for all your help

Tell me the problem, I will try tell you the solution




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users