Nate
Nate

Reputation: 28334

finfo_file() returns wrong mime type for some images?

I allow image uploads on my website and need to get the file types of uploaded files so that I can check to see if the type is whitelisted and allowed to be uploaded.

I've been doing this by using finfo_file(), but it doesn't seem to work in all cases. Here's my code:

// get the mime type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime  = finfo_file($finfo, $filePath);
echo $mime;

http://ecx.images-amazon.com/images/I/41q8koaxM0L.jpg

Try running the above code on the image above and it will say the MIME type is application/octet-stream when it is clearly a JPG file.

Why is finfo_file() not working with the test image? How can I fix whatever the problem is so that I can accurately get the file type?

Upvotes: 2

Views: 1482

Answers (1)

Kevin
Kevin

Reputation: 41885

Alternatively, you could also use exif_imagetype() in conjunction to image_type_to_mime_type(). Example:

function get_mime($url) {
    $image_type = exif_imagetype($url);
    return image_type_to_mime_type($image_type);
}

echo get_mime('http://ecx.images-amazon.com/images/I/41q8koaxM0L.jpg'); // image/jpeg

Upvotes: 2

Related Questions