为什么在 PHP 中不推荐使用 mime_content_type()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1263957/
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
Why is mime_content_type() deprecated in PHP?
提问by Josiah
I'm just curious to know why mime_content_type()is now considered deprecated.
我只是想知道为什么mime_content_type()现在被认为已弃用。
This method for determining the mime type is much easier than the replacement Fileinfofunctionality.
这种确定 mime 类型的方法比替换Fileinfo功能要容易得多。
采纳答案by Alix Axel
I guess it's because Fileinfo can return more information about files.
我猜是因为 Fileinfo 可以返回有关 files 的更多信息。
EDIT: Here is a replacement hack:
编辑:这是一个替代黑客:
function _mime_content_type($filename) {
$result = new finfo();
if (is_resource($result) === true) {
return $result->file($filename, FILEINFO_MIME_TYPE);
}
return false;
}
回答by Adam
The method is not deprecated!
该方法不被弃用!
It once was incorrectly marked as deprecated in the manual, but it has been fixed https://bugs.php.net/bug.php?id=71367on the 14th of January 2016.
However, at the moment, it is still incorrectly marked deprecated in the German, Spanish and Chinese manual.
它曾经在手册中被错误地标记为已弃用,但已于 2016 年 1 月 14 日修复https://bugs.php.net/bug.php?id=71367。
然而,目前,它仍然是错误的在德文、西班牙文和中文手册中标记为弃用。
Feel free to use mime_content_type()whenever you like :).
随时可以随意使用mime_content_type():)。
回答by Alix Axel
Another way is to pass to the constructor constant FILEINFO_MIME.
另一种方法是传递给构造函数常量FILEINFO_MIME。
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->file('path/filename');
回答by Timo Tijhof
Using finfo_fileand finfo_open, and FILEINFO_MIME_TYPE:
使用finfo_fileand finfo_open, and FILEINFO_MIME_TYPE:
finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $filename );
Here's a small wrapper to cover different PHP environments, derived from CSSMin.php in MediaWiki?1.20:
这是一个覆盖不同 PHP 环境的小包装器,派生自MediaWiki?1.20 中的 CSSMin.php:
function getMimeType( $filename ) {
$realpath = realpath( $filename );
if ( $realpath
&& function_exists( 'finfo_file' )
&& function_exists( 'finfo_open' )
&& defined( 'FILEINFO_MIME_TYPE' )
) {
// Use the Fileinfo PECL extension (PHP 5.3+)
return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $realpath );
}
if ( function_exists( 'mime_content_type' ) ) {
// Deprecated in PHP 5.3
return mime_content_type( $realpath );
}
return false;
}
EDIT:Thanks @Adamand @ficuscrfor clarifying that this function was, in fact, not deprecated.
编辑:感谢@Adam和@ficuscr澄清该功能实际上并未弃用。
As of MediaWiki 1.30, the above code was essentially changed (back) to:
从 MediaWiki 1.30 开始,上面的代码基本上更改(返回)为:
function getMimeType( $filename ) {
return mime_content_type( $filename );
}
回答by Jofator
This works:
这有效:
if (!function_exists('mime_content_type')) {
function mime_content_type($filename)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimeType;
}
}

