Jump to content

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.