Best way to get type of photo file extension in PHP

I am updating my section for uploading photos on my website, I try to find out as much as possible so that I can do it reliably, but also with better performance. My site has 15-20 photos uploaded per minute usually

So, is this method reliable for getting the type of photo file such as jpg, gif, png?

 $fileType = $_FILES['image']['type'];
+3
source share
4 answers

File extensions are or may be at least. It is definitely better not to rely on / trusted user data. Also, as written in the PHP manual:

$_ FILES ['userfile'] ['type'] - mime , . "image/gif". mime, , PHP .

.

:

PHP getimagesize() , 2 IMAGETYPE_XXX ( , ), . GD, - image_type_to_extension() image_type_to_mime_type().

, - :

$imageData = getimagesize($_FILES['userfile']['tmp_name']);
// $imageData[2] will contain the value of one of the constants
$mimeType = image_type_to_mime_type($imageData[2]);
$extension = image_type_to_extension($imageData[2]);

, exif, [exif_imagetype()][5] , 2 getimagesize(), .

GD , PHP. Imagick , mime fileinfo ( 5.3, btw).

+13

exif_imagetype() - , , , URL-.

- , getimagesize() 2 exif_imagetype() .

$type = @exif_imagetype($_FILES['image']['tmp_name']);

if (($type >= 1) && ($type <= 3))
{
    echo 'Valid image (GIF, JPG or PNG).';
}

else
{
    unlink($_FILES['image']['tmp_name']); // delete it
}
+2

getimagesize exif_imagetype(),

['mime'], finfo_file ( extension_loaded('fileinfo')),

, mime_content_type (if function_exists('mime_content_type')).

mime, , pathinfo($path, PATHINFO_EXTENSION).

+2

:

mime , . [...] mime, , PHP, .

, , , , jpeg.

, , Imagick:: identImage . GD, , ​​ .

mime fileinfo, finfo_file()

+1

Source: https://habr.com/ru/post/1715222/


All Articles