Check file is image php

Native way to get the mimetype:

For PHP < 5.3 use mime_content_type()
For PHP >= 5.3 use finfo_open() or mime_content_type()

Alternatives to get the MimeType are exif_imagetype and getimagesize, but these rely on having the appropriate libs installed. In addition, they will likely just return image mimetypes, instead of the whole list given in magic.mime.

While mime_content_type is available from PHP 4.3 and is part of the FileInfo extension (which is enabled by default since PHP 5.3, except for Windows platforms, where it must be enabled manually, for details see here).

If you don't want to bother about what is available on your system, just wrap all four functions into a proxy method that delegates the function call to whatever is available, e.g.

function getMimeType($filename)
{
    $mimetype = false;
    if(function_exists('finfo_open')) {
        // open with FileInfo
    } elseif(function_exists('getimagesize')) {
        // open with GD
    } elseif(function_exists('exif_imagetype')) {
       // open with EXIF
    } elseif(function_exists('mime_content_type')) {
       $mimetype = mime_content_type($filename);
    }
    return $mimetype;
}

function isImage($image){
    $extension = pathinfo($image, PATHINFO_EXTENSION);
    $imgExtArr = ['jpg', 'jpeg', 'png'];
    if(in_array($extension, $imgExtArr)){
        return true;
    }
    return false;
}

This function will return true or false on the basis of the extension of files. It provides true if the file is an image or returns false if the file is not an image. You can pass more image file extensions to $imgExtArr variable.

Most of us would have come across the common solutions in PHP to check if the uploaded file is an image, you might have tried the usual way of checking the extensions and accepting the file to be a valid image, what if someone sinister uploaded a malicious script just by changing the extension of the file to .jpg or .gif ? . I bet most of us would have used the following to validate the image uploaded in PHP.

Check if uploaded image is valid with getimagesize( ) in PHP

$imagesizedata = getimagesize($file);
if ($imagesizedata ) {
//do something
}

but PHP docs says not to use getimagesize( ) to check that a given file is a valid image. it’s true that getimagesize( ) returns an array with some random values for image height & width, when the given file is not a valid image.

Check if uploaded image is valid by checking the extensions

$type=$_FILES[ 'image' ][ 'type' ];     
$extensions=array( 'image/jpeg', 'image/png', 'image/gif' );
if( in_array( $type, $extensions )){
//do something
}

but here you are checking only the extensions, like I said earlier, anyone can rename a file with a different content into jpg or gif and upload them without any problem.

Check if uploaded image is valid with exif data and fileinfo in PHP

Now let’s try something to check if the given image is valid, but before going into the example, let me tell you, even the following functions in PHP will not validate an image correctly.

  • finfo_file( )
  • mime_content_type( )
  • exif_imagetype( )

All the above functions will return the mime/image type for the given image, but they will fail as well if we upload a file after changing the extension or cheat with the first few bytes of image headers. Here is the example I was talking about. I have the following content in a file and saved it as motion.gif

GIF89a<

and used the following PHP code to check the validity of the image, you can see I have tried the four commonly recommended PHP functions to check if the given image is valid and each one of them failed.

";
echo $filename . ": "  . mime_content_type( $filename )  . "
"; echo "exif: " . exif_imagetype( $filename ) . "
"; print_r( getimagesize( $filename ) ); finfo_close( $finfo );

this is the output of the above code

Check file is image php
so all the above mentioned solutions to check if the uploaded files is a valid image will deceive us in the end, the only best solution is to use imagecreate function in PHP, we have separate functions for jpg, gif , png or to create image with the given string or content of the file. imagecreate function will return false when it fails to create an image with the given image file., so you can check if the given file is a valid jpg or gif or png.

here is the code I tried, to validate the uploaded image files.

You can experiment yourself with various images and cheating with the first few headers of jpg, gif or png.

How do you check if a file is an image in PHP?

The getimagesize() function will determine the size of any supported given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondent HTTP content type.

How to check file is video or image in PHP?

“php check if file is video” Code Answer.
$mime = mime_content_type($file);.
if(strstr($mime, "video/")){.
// this code for video..
}else if(strstr($mime, "image/")){.
// this code for image..

How check image is set or not in PHP?

From PHP: bool file_exists ( string $filename ) As of PHP 5.0. 0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

How can I tell if image is PDF or PHP?

You can simply use an OR statement, i.e. return ($file['content-type'] == 'image/*' || $file['content-type'] == 'application/pdf'); This is assuming you still just want to return true/false. So the caller will know that the file was either a PDF or an image.