What's the difference between exif_imagetype and finfo when trying to determine file type of an uploaded image? Which is more secure?

I've seen the following two code snippets to check file type. What's the difference between the two and which is more reliable/secure?

$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
	$finfo->file($_FILES['upfile']['tmp_name']),
	array(
		'jpg' => 'image/jpeg',
		'png' => 'image/png',
		'gif' => 'image/gif',
	),
	true
)) {
	throw new RuntimeException('Invalid file format.');
}

Open in new window


$type = exif_imagetype($file['tmp_name']);
if ($type) {
	$extension = image_type_to_extension($type);
	if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_JPEG || $type == IMAGETYPE_PNG) {

	} else {
	  $this -> msg = 'Please upload image with the following types: JPG, PNG, GIF';
	}
} else {
	$this -> msg = 'Please upload image file';
}

Open in new window

Zoe ZoeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
The most reliable and secure method is to create an image resource from the uploaded file, create a new image file, and store the new file, discarding the old file.  Attacks via images are kind of a decade ago, and we don't let that happen any more.  Filter Input Escape Output.

A code sample might look like this.  If the image extension is not what you expect from the image, you're almost certainly dealing with attack data.  Just don't use that stuff.
// ACQUIRE THE ORIGINAL IMAGE
$image_ext = explode('.', $image_url);
$image_ext = end($image_ext);
$image_ext = trim(strtoupper($image_ext));
switch($image_ext)
{
    case 'JPG' :
    case 'JPEG' :
        $image = @imagecreatefromjpeg($image_url);
        if ($image) break;

    case 'PNG' :
        $image = @imagecreatefrompng($image_url);
        if ($image) break;

    default : trigger_error("UNKNOWN IMAGE TYPE: $image_url", E_USER_ERROR);
}

Open in new window

Julian HansenCommented:
To add to what Ray said.

exif_imagetype is the preferred method - if that is not available you can also use getimagesize which returns an array of info - index 2 of which is relevant for checking the image type.

exif_imagetype is faster than getimagesize

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.