Why is mime_content_type() deprecated in PHP?

后端 未结 5 1230
悲&欢浪女
悲&欢浪女 2020-11-28 11:06

I\'m just curious to know why mime_content_type() is now considered deprecated.

This method for determining the mime type is much easier than the replacement Fileinf

5条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 11:43

    Using finfo_file and 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:

    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 );
    }
    

提交回复
热议问题