7

Possible Duplicate:
How do I determine the extension(s) associated with a MIME type in PHP?

I'm aware of how to get the MIME type of a given file, but is there a way to do it given only an extension?

Example input:

jpg

Example output:

image/jpeg
Community
  • 1
  • 1
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
  • Does [this comment](http://us3.php.net/manual/en/function.mime-content-type.php#107798) from the PHP docs help? – nickb Jul 18 '12 at 15:19
  • 1
    I do not think it's a duplicate, as drrcknlsn wants to get the mime type from the extension while the question (which this is marked as duplicate of) does the opposite, finding the extension for a mime type. – Alex Oct 19 '15 at 10:59
  • 1
    Clearly, as @Alex says. How is this a duplicate of the linked answer, if that one wants the (*list of*!) extensions for a MIME type, while this one needs the inverse function: get *the* MIME type for *an* extension?! – Sz. Feb 09 '17 at 23:53
  • should help : https://gist.github.com/nimasdj/801b0b1a50112ea6a997 – Cyril Jacquart Nov 27 '18 at 13:35

3 Answers3

3

try this to get info about which extension is which mime type, but of course please be aware that this won't be very accurate (e.g. possible gif file on png extension..)

Community
  • 1
  • 1
Jerzy Zawadzki
  • 1,975
  • 13
  • 15
  • I ended up rolling my own method, but thanks for letting me know about `/etc/mime.types`! That was the ticket. – FtDRbwLXw6 Jul 18 '12 at 20:25
2

The most elegant solution I've seen for Apache environments is http://www.php.net/manual/en/function.mime-content-type.php#107798

Ross Smith II
  • 11,799
  • 1
  • 38
  • 43
  • Even if you aren't using Apache, it would be easy enough to get the `mime.types` file and query directly against it. I was going to suggest creating a temporary file and querying against it in the normal way, but loading a list of mime types and doing a lookup would be better. – AndrewR Jul 18 '12 at 15:26
1

I don't have the Fileinfo extension installed, but from PHP documentation, it seems like Fileinfo might give you that information, which was previously given by the deprecated function mime_content_type()

http://www.php.net/manual/en/function.mime-content-type.php

http://www.php.net/manual/en/ref.fileinfo.php

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Tchoupi
  • 14,560
  • 5
  • 37
  • 71