为什么PHP中不推荐使用mime_content_type()?

Jos*_*iah 24 php deprecated mime-types

我只是想知道为什么现在认为mime_content_type()被弃用了.

这种用于确定mime类型的方法比替换Fileinfo功能容易得多.

Ada*_*dam 39

该方法不推荐使用!

它曾在手册中被错误地标记为已弃用,但已在2016年1月14日修复了https://bugs.php.net/bug.php?id=71367. 但是,此刻,它仍然是错误的德语,西班牙语和中文手册中标注弃用.

mime_content_type()随时随地使用:).


Ali*_*xel 23

我想这是因为Fileinfo可以返回有关文件的更多信息.

编辑:这是一个替代黑客:

function _mime_content_type($filename) {
    $result = new finfo();

    if (is_resource($result) === true) {
        return $result->file($filename, FILEINFO_MIME_TYPE);
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 虽然这可能是真的,但事实仍然是配置起来要困难得多,并且需要花费更多精力才能使用.难道它不能保持不受限制,只是利用finfo功能吗? (11认同)
  • @Josiah函数不被弃用 - 请参阅我的回答. (5认同)

小智 6

另一种方法是传递给构造函数常量FILEINFO_MIME.

$finfo = new finfo(FILEINFO_MIME);
$type  = $finfo->file('path/filename');
Run Code Online (Sandbox Code Playgroud)


Tim*_*hof 6

使用finfo_fileand finfo_open, and FILEINFO_MIME_TYPE

finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $filename );
Run Code Online (Sandbox Code Playgroud)

这是一个覆盖不同 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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:感谢@Adam@ficuscr澄清该功能实际上并未弃用

从 MediaWiki 1.30 开始,上面的代码基本上更改(返回)为:

function getMimeType( $filename ) {
        return mime_content_type( $filename );
}
Run Code Online (Sandbox Code Playgroud)