How to get MIME-type of an image with file_get_contents in PHP

后端 未结 3 1683
梦谈多话
梦谈多话 2021-02-01 22:58

I need to get the MIME type of an image, but I only have the body of the image which I\'ve got with file_get_contents. Is there a possibility to get the MIME type?<

3条回答
  •  隐瞒了意图╮
    2021-02-01 23:20

    Be very careful what you do by checking only the Mime Type! If you really want to be sure that an image is actually an image, the safest way to do this is open it with an image manipulation library and write it with the library. This will both fail if the image is actually malicious code and guarantee that you are actually writing an image file to the disk. Just as an example for this, you can easily trick MIME into thinking that some malicious code is GIF.

    To answer your questions more directly, use the FileInfo PECL module.

提交回复
热议问题