Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate that a file is a picture in PHP

If a file is uploaded to the server, is there a way using PHP, to make sure that it's actually a picture and not just a file with a .jpg or .gif extension?

like image 429
jd. Avatar asked Oct 17 '09 01:10

jd.


People also ask

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

Check file is image or not in php 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.

What is image validation?

Image validation occurs in Gateway Server image processing threads. Image Compliance collects the validation data so it can apply the specified set of image validation rules, which allows a rule set to execute the same image test using different threshold criteria.

How can I get image type in PHP?

The imagetypes() function is an inbuilt function in PHP which is used to return the image types supported by the PHP inbuilt installed library. Parameters: This function does not accept any parameter.


1 Answers

Using (part) of the GD library.

PHP: GD - Manual

array getimagesize ( string $filename [, array &$imageinfo ] )

The first element of the array will be 0 if there is no image. PHP: getimagesize

If you don't have GD installed (most of the time you will), you can read the file header as Shane mentioned.

EDIT: Actually, as Neal pointed out in the comments, the GD library is not even required to use this function. So use it.

like image 86
snicker Avatar answered Oct 06 '22 00:10

snicker