We can use the php’s inbuilt function getimagesize to get the informations about an file, including its type. While checking for the type we can can also check the file is image or not. So by using getimagesize we can check if file is an image in php

Here is the code to do so

function is_image($path)
{
    $a = getimagesize($path);
    $image_type = $a[2];
     
    if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
    {
        return true;
    }
    return false;
}

Pass the file to the function is_image()
$a[0] and $a[1] are the width and height of the image.
$a[2] has the image type.

Hope this helps to check if file is an image in php

2 Comments on “Check if file is an image in php

  1. Johnathanher
    May 14, 2014

    Ԍreat information. Lucky mе I dikscovered yiur
    site by chance (stumbleupon). І’ve book-marked іt foг
    later!

  2. Stanton
    May 24, 2014

    Thanks for sharing your thoughts. Regards

Leave a Reply

Your email address will not be published. Required fields are marked *