Jump to content

checking is jpg is really a jpeg


onlyican

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
Link to comment
https://forums.phpfreaks.com/topic/19785-checking-is-jpg-is-really-a-jpeg/
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 ;-)
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

Archived

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

×
×
  • Create New...

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.