8

I use var_dump(@$_FILES['file']['type']) to test file type I uploaded

First, I uploaded an exe file called "uninstall.exe", and it returned

"string 'application/octet-stream' (length=24)"

Then, I renamed this file to uninstall.png, it returned

string 'image/png' (length=9)

My conclusion is: $_FILES['file']['type'] only check file extension, not the original file type.

The following code is from w3cschool:

$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))

I think $_FILES["file"]["type"] in above codes is unnecessary, we can just check file extension using explode() and in_array

I'm just a php beginner, can someone confirm my idea? Thanks!

Eathen Nutt
  • 1,433
  • 5
  • 19
  • 27

3 Answers3

7

You're absolutely correct. The MIME type is provided by the client and you cannot guarantee it is cor­rect. For that matter, so is the file extension. If you need to be completely sure, you need to look at the file contents.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • I'm glad my idea got confirmed, someone said:"the extension of the file and 'file type' can be differ, so someone can not upload executable file with .png extension with $_FILES["file"]["type"]." I think he is wrong – Eathen Nutt Mar 24 '13 at 22:54
  • @user1970939: Well, someone actually could provide a file named `file.exe` with a MIME type of `image/png`. They're both provided by the client and are not required to be consistent. – icktoofay Mar 24 '13 at 22:59
7

If you want to be sure that an image was uploaded, use getimagesize, that returns 0 for non-images.

darthmaim
  • 4,970
  • 1
  • 27
  • 40
  • and some JPEGs even. Mostly JPEGs that come from digital cameras. – Emery King Mar 24 '13 at 23:04
  • thanks for mentioning getimagesize – Eathen Nutt Mar 24 '13 at 23:05
  • 1
    @darthmaim absolutely incorrect! `getimagesize` can and will return successfully for some non-images. See [here](http://php.net/manual/en/function.getimagesize.php) – jdrake Nov 20 '17 at 11:24
  • 1
    It says in the official PHP docs NOT to use `getimagesize`. "Do not use getimagesize() to check that a given file is a valid image. Use a purpose-built solution such as the Fileinfo extension instead." – Edward Jul 28 '19 at 22:11
1

You should be using a wrapper of GD or Imagick extensions. A very good one is WideImage.

Lucas Freitas
  • 194
  • 12