[PHP] Mime Types & .PNG images

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

I am having a little trouble with getting the mime type (or any details) of the image type .PNG.

Whenever I upload the image and run this script:

PHP:
<?php

     $file_details = @getimagesize($_FILES['image']['tmp_name']);
     print $file_details;

?>

I get this:



You can see it in action here: Click Here

The image I am trying to upload is simply a colour to test:



Thanks in advance!


- Conrad11
 
Last edited:
That's because getimagesize() returns an array of details about the image, not a single value:

Returns an array with 4 elements.
Index 0 contains the width of the image in pixels.
Index 1 contains the height.
Index 2 is a flag indicating the type of the 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. These values correspond to the IMAGETYPE constants that were added in PHP 4.3.0.
Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag
http://uk.php.net/manual/en/function.getimagesize.php

So you want to be looking at

Code:
print $file_details[2];
// should print "3".
Revealing the detected image type - it's not the actual mimetype of the image, that would be image/png. :)
 
and avoid using the error suppression operator, there really is no need to use it. Only in very rare circumstances does it require use - i.e. when a custom error handler is in place - and that is most certainly not one of those situations.
 
The reason I use the error supprision, is that fact that its used in file validation, I use the function to get details of the image. And if by chance the user uploads a txt file, does the getimagesize function not produce an error?
 
Back
Top Bottom