How can I determine a file's true extension/type programmatically?

前端 未结 11 1336
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 17:06

I am working on a script that will process user uploads to the server, and as an added layer of security I\'d like to know:

Is there a way to detect a file\'s true e

11条回答
  •  不知归路
    2020-12-01 17:50

    Others have already mentioned FileInfo, which I think is the correct solution, but I'll add this just in case you can't use that one for some reason. Most (all?) *nix distros include a command called file that when run on a file will output its type. It has a switch to output in human readable format (default) or the MIME type. You could have your script invoke this program on the uploaded file and read the result. Again, this is not the preferred approach. If you're on Windows, this utility is available through Cygwin.

提交回复
热议问题