File upload limits a specific file type

I am doing a script file upload that only jpg permits.
the script that I use to verify

$size = getimagesize($filename);
$size['mime'];

This works in most cases. However, if I have a “gif” file and I renamed the extension to “jpg”, it tricks the system, since the mime type for this file is displayed as jpg.

How can I prevent this?

So allowed jpg and png gif is prohibited

+3
source share
4 answers

Instead $size['mime'](which, as you understand, is a MIME type and therefore not completely reliable), use $size[2].

Introductory entry says it contains

one of the IMAGETYPE_XXX constants indicating the type of 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

, , .

+3

, "gif", "jpg", , mime jpg.

( ) pathinfo():

$file =
 pathinfo("myFakeGifImage.jpg", PATHINFO_BASENAME); // returns myFakeGifImage

, getimagesize(), , .

+1

jpegs, imagecreatefromjpeg, FALSE jpegs.

0

PHP MIME- , .. mime_content_type. , .

0
source

All Articles