Check file is image php
Native way to get the mimetype: Show
For PHP < 5.3 use 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 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.
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 PHPNow 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.
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< |