这种方法没有被弃用!
它曾经在手册中被错误地标记为已弃用,但在2016年1月14日修复了此问题https://bugs.php.net/bug.php?id=71367。
然而,目前,在德语、西班牙语和中文手册中仍被错误标记为已弃用。
随意使用mime_content_type()
,它不会有任何问题 :)
function _mime_content_type($filename) {
$result = new finfo();
if (is_resource($result) === true) {
return $result->file($filename, FILEINFO_MIME_TYPE);
}
return false;
}
使用 finfo_file
和 finfo_open
,以及 FILEINFO_MIME_TYPE
常量:
finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $filename );
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 @Adam and @ficuscr for clarifying that this function was, in fact, not deprecated.
As of MediaWiki 1.30, the above code was essentially changed (back) to:
function getMimeType( $filename ) {
return mime_content_type( $filename );
}
FILEINFO_MIME
传递给构造函数。$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->file('path/filename');