33

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 Fileinfo functionality.

hakre
  • 193,403
  • 52
  • 435
  • 836
Josiah
  • 3,232
  • 3
  • 25
  • 25
  • 4
    I just looked at the page for mime_content_type in the manual and it doesn't say it's deprecated! It even includes PHP 7 as a supported version of PHP – GordonM Jul 07 '16 at 14:33
  • 11
    **Its not deprecated** see my answer. – Adam Sep 25 '16 at 10:42

4 Answers4

58

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=71367 on the 14th of January 2016. However, at the moment, it is still incorrectly marked deprecated in the German, Spanish and Chinese manual.

Feel free to use mime_content_type() whenever you like :).

rinogo
  • 8,491
  • 12
  • 61
  • 102
Adam
  • 25,960
  • 22
  • 158
  • 247
26

I guess it's because Fileinfo can return more information about 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;
}
Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 11
    Although this is possibly true, the fact remains that it is much harder to configure and takes more effort to use. Could it not remain un-depricated and just utilize the finfo functions? – Josiah Aug 12 '09 at 02:55
  • I use a similar hack to determine the file-type of files but firstly use the mime_content_type() method because I have found it to be more reliable across servers. However i'm still perplexed as to why this method is now deprecated? – Josiah Aug 12 '09 at 03:01
  • I agree with both your comments, they could have just rewrited the function to rely on the finfo class. – Alix Axel Aug 12 '09 at 05:57
  • 1
    $result = new finfo(); not work, should be new finfo(FILEINFO_MIME_TYPE); – jk jk Jul 05 '13 at 06:21
  • @jkjk: It should work, that argument is optional. Plus, I'm pretty sure I have tested this. – Alix Axel Jul 05 '13 at 11:01
  • 6
    @Josiah the function is not deprecated - see my answer. – Adam Sep 24 '16 at 12:22
  • `finfo` not found in latest php – ina Jan 24 '21 at 11:37
6

Another way is to pass to the constructor constant FILEINFO_MIME.

$finfo = new finfo(FILEINFO_MIME);
$type  = $finfo->file('path/filename');
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
6

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 );
}
Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
  • 1
    Was not actually deprecated in 5.3, just bad documentation/mistake. In fact support is there for 5.3 > and into 7.0. – ficuscr Jul 21 '17 at 17:21