23

I am using the mime_content_type() function for file upload, it works fine on localhost however I'm encountering the following error on my live server:

Fatal error: Call to undefined function mime_content_type()

John
  • 1
  • 13
  • 98
  • 177
user1898114
  • 231
  • 1
  • 2
  • 6
  • 6
    http://www.php.net/manual/en/fileinfo.installation.php -> read the first line of the first comment. – Salman A Feb 11 '13 at 09:19
  • 1
    Also note that the function is deprecated: http://php.net/mime_content_type Use http://www.php.net/manual/en/ref.fileinfo.php instead. – Jan Hančič Feb 11 '13 at 09:20
  • Prior to PHP 5.3.0, mime_content_type() was only available via the PECL fileinfo extension - what version of PHP are you running? – Mark Baker Feb 11 '13 at 09:21
  • It's documented in the manual. Not deprecated or documented as requiring anything special. But since it doesn't work, I have written my own function. – David Spector Sep 07 '20 at 13:15

6 Answers6

18

Update:

mime_content_type() is no longer deprecated, php7 has support for this function now.

Earlier version of my answer:

mime_content_type() is deprecated, probably because [fileinfo][1] can give you those information about the file and more.

You can use finfo() like shown below,

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

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

    return false;
}

Ref: https://stackoverflow.com/a/1263977/1161412

[1]: http://php.net/manual/en/class.finfo.php

Community
  • 1
  • 1
Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92
  • 3
    finfo is a pecl extension, so won't work if the extension is not installed – Okneloper Apr 13 '16 at 08:15
  • 1
    @Okneloper Not anymore, quoating from the installation doc "`This extension is enabled by default as of PHP 5.3.0. Before this time, fileinfo was a PECL extension but is no longer maintained there`" http://php.net/manual/en/fileinfo.installation.php – Nagendra Rao Apr 14 '16 at 10:07
  • 2
    yet the installation of 5.6 I tested it on gave me a "Fatal error: Class 'finfo' not found" – Okneloper Apr 14 '16 at 10:55
  • 1
    @Okneloper Is it a windows platform? if it is then you'll need to do this, `Windows users must include the bundled php_fileinfo.dll DLL file in php.ini to enable this extension.` – Nagendra Rao Apr 14 '16 at 12:10
  • no it's not. It's a CentOS with Cpanel. Thanks for the effort though. – Okneloper Apr 14 '16 at 13:38
  • http://php.net/manual/en/function.mime-content-type.php "mime_content_type (PHP 4 >= 4.3.0, PHP 5, PHP 7)" (I.E. not deprecated) – HoldOffHunger Jun 22 '16 at 00:29
  • @HoldOffHunger It was marked as deprecated on the same documentation page at the time I wrote this answer. I guess they are supporting it now in php7. – Nagendra Rao Jun 22 '16 at 12:47
16

You must have the mime_magic extension on. Check your php.ini and look in phpinfo(). By the way this function has been deprecated as the PECL extension Fileinfo provides the same functionality (and more) in a much cleaner way.

Windows users must include the bundled php_fileinfo.dll DLL file in php.ini to enable this extension.

The libmagic library is bundled with PHP, but includes PHP specific changes. A patch against libmagic named libmagic.patch is maintained and may be found within the PHP fileinfo extensions source.

Read more

John
  • 1
  • 13
  • 98
  • 177
Techie
  • 44,706
  • 42
  • 157
  • 243
4

PHP 8

  1. Edit your php.ini file and uncomment extension=fileinfo.

  2. Restart your HTTP server (e.g. Apache).

  3. echo mime_content_type($path_absolute.$file);//Outputs: application/pdf

PHP 7

  1. Edit your php.ini file and uncomment extension=php_fileinfo.dll.

  2. Restart your HTTP server (e.g. Apache).

  3. echo mime_content_type($path_absolute.$file);//Outputs: application/pdf

John
  • 1
  • 13
  • 98
  • 177
  • Thank you everywhere just saying it has been deprecated but after adding this gets resolved. – Aazad R khan Nov 02 '22 at 12:03
  • @AazadRkhan Who is saying `mime_content_type()` is deprecated? Certainly not the documentation! https://www.php.net/mime_content_type – John Nov 02 '22 at 13:50
  • most of the link are saying **mime_content_type()** get deprecated. but after some research I found in here – Aazad R khan Dec 08 '22 at 04:44
1

If you are on shared hosting, chances are that the fileinfo PHP extension is either not enabled or installed.

In the case where it's not enabled, navigate to the Software section of CPanel (consult documentation of your control panel if you're not using CPanel) and click Select PHP Version (or something related to that) and enable the extension by checking its box and saving your action.

If it's not installed, the extension won't be part of the PHP extensions at cPanel > Software > Select PHP Version > Extensions, edit your php.ini file and uncomment extension=php_fileinfo.dll if you're on Windows. Consult your hosting provider's docs if any of these don't work.

Nayi
  • 65
  • 1
  • 10
-1

I changed my php version from 7.1 to 5.6 it works for me

shini
  • 1
-2

Likely missing \php\extras\magic.mime file.

Tomiwa Adefokun
  • 287
  • 4
  • 7