Search code examples
phpuploadmime-typesfile-type

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


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 me what filetype it is based on the extension. However, I'm afraid that users could rename a zip file as somezipfile.png and store it, thus keeping a zip file on my server. Is there any reasonable way to open an uploaded file and "check" to see if it truly is of the said filetype?


Solution

  • Magic number. If you can read first few bytes of a binary file you can know what kind of file it is.