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

后端 未结 9 1818
野性不改
野性不改 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:05

    Sort of. Most file types have some bytes reserved for marking them so that you don't have to rely on the extension. The site http://wotsit.org is a great resource for finding this out for a particular type.

    If you are on a unix system, I believe that the file command doesn't rely on the extension, so you could shell out to it if you don't want to write the byte checking code.

    For PNG (http://www.w3.org/TR/PNG-Rationale.html)

    The first eight bytes of a PNG file always contain the following values:

    (decimal) 137 80 78 71 13 10 26 10

    (hexadecimal) 89 50 4e 47 0d 0a 1a 0a

    (ASCII C notation) \211 P N G \r \n \032 \n

提交回复
热议问题