What is the best way to determine whether or not a file is an image in PHP?

前端 未结 12 481
有刺的猬
有刺的猬 2021-01-22 07:25

I have a sever which people can upload files to. The problem is that some of the filenames are mangled (dont have any extension) and so I cannot immediately determine file type.

12条回答
  •  清酒与你
    2021-01-22 07:46

    You can use the Fileinfo extension: http://www.php.net/manual/en/function.finfo-file.php

    finfo_file() uses magic bytes and does not have to load the whole image into memory. The result is a string with the corresponding MIME type, e.g.:

    • text/html
    • image/gif
    • application/vnd.ms-excel

提交回复
热议问题