php 如何检查 MIME 类型是 JPG、PNG、BMP 还是 GIF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16568343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check whether a MIME type is of JPG, PNG, BMP or GIF?
提问by Lajos Arpad
I have written this code:
我写了这段代码:
$filename = "some/path/where/the/file/can/be/found.some_extension";
$buffer = file_get_contents($filename);
$finfo = new finfo(FILEINFO_MIME_TYPE);
var_dump($finfo->buffer($buffer));
finfo_close($finfo);
Possible outputs were of:
可能的输出是:
"image/jpeg", "image/png", "image/gif", "image/x-ms-bmp"
I would like to know, what are the possible outputs of $finfo->buffer($buffer)
, if the file is a png, gif, bmp, or jpg?
我想知道,$finfo->buffer($buffer)
如果文件是 png、gif、bmp 或 jpg,可能的输出是什么?
I have seen herea returnMIMEType
function, which, for instance will not detect "image/x-ms-bmp"
to be a bmp.
采纳答案by Lajos Arpad
We can view the possible MIME types of file extensions by searching for the file extension at the link provided by Pitchinnate. For instance BMP MIME types can be found at: filext.com/file-extension/BMP
我们可以通过在 Pitchinnate 提供的链接中搜索文件扩展名来查看可能的 MIME 类型的文件扩展名。例如,可以在以下位置找到 BMP MIME 类型:filext.com/file-extension/BMP