Brian
Brian

Reputation: 27392

ensure that uploaded file is image in PHP

Is there a good method for making sure that an uploaded file is an image in PHP?

Upvotes: 3

Views: 1699

Answers (4)

Stefan
Stefan

Reputation: 2068

Here:

if(strpos($_FILES["fieldname"]["type"], "image") !== FALSE) {
    // is an image
} else {
    // is no image
}

Upvotes: -2

djn
djn

Reputation: 3948

Pass the received file to the getimagesize() function. If the file is an image (of supported type) you'll get back an array, with the image type stored as third member of the array. If the file is not an image result will be false.

Upvotes: 1

Devin Ceartas
Devin Ceartas

Reputation: 4829

One method that goes beyond asking it if it an image using MIME, FileInfo, etc. is to try and do something with it as an image -- load up with GD or ImageMagik and use that to write a new copy to the desired location.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Use the Fileinfo functions.

Upvotes: 2

Related Questions