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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:22:44  来源:igfitidea点击:

How to check whether a MIME type is of JPG, PNG, BMP or GIF?

phpmime-typesfileinfo

提问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 returnMIMETypefunction, which, for instance will not detect "image/x-ms-bmp"to be a bmp.

我在这里看到一个returnMIMEType函数,例如它不会检测"image/x-ms-bmp"为 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

回答by riju.srk

In thisthread, you can find some useful suggestions and functions for your situation.

线程中,您可以找到一些适用于您的情况的有用建议和功能。