How do I tell if someone's faking a filetype? (PHP)

后端 未结 9 1961
野性不改
野性不改 2021-02-08 00:31

I\'m programming something that allows users to store documents and pictures on a webserver, to be stored and retrieved later. When users upload files to my server, PHP tells m

9条回答
  •  轮回少年
    2021-02-08 01:12

    If you are only dealing with images, then getimagesize() should distinguish a valid image from a fake one.

    $ php -r 'var_dump(getimagesize("b&n.jpg"));'
    array(7) {
      [0]=>
      int(200)
      [1]=>
      int(200)
      [2]=>
      int(2)
      [3]=>
      string(24) "width="200" height="200""
      ["bits"]=>
      int(8)
      ["channels"]=>
      int(3)
      ["mime"]=>
      string(10) "image/jpeg"
    }
    
    $ php -r 'var_dump(getimagesize("/etc/passwd"));'
    bool(false)
    

    A false value from getimagesize is not an image.

提交回复
热议问题