2

I'm creating a script to upload image file. There are two ways to verify if the file is an image.

First one is by checking the extension:

$extension = strtolower(substr($filename, strrpos($filename, '.') + 1, strlen($filename) - strrpos($filename, '.')));
if ($extension == "jpg") // UPLOAD

Another one is by checking the mime type:

$imageinfo = getimagesize($filename);
if ($imageinfo['mime'] == "image/jpeg") // UPLOAD

Which one is the better method to verify if the file is an image?

Trondro Mulligan
  • 485
  • 3
  • 19

1 Answers1

1

Use the first one, because not every MIME type has a fixed file extension. Also, MIME types like application/octet-stream can refer to multiple file extensions. and also you can use pathinfo() for getting file extension.

check below links

How to extract a file extension in PHP?

Not every MIME type has a fixed file extension...

Community
  • 1
  • 1
msk
  • 481
  • 2
  • 12