2

PHP has a built in mime_content_type that returns a MIME type (ex: image/jpeg) for a file and extension input (ex: image.jpg). I've looked for a function that goes from the MIME type to the extension, but I don't think it exists.

Does it? If not, is there an easy way to go from "image/jpeg" to ".jpg" or "image/png" to ".png", or something to that effect?

Thanks!

K. Barresi
  • 1,275
  • 1
  • 21
  • 46
  • 3
    Do you have a finite set of them? Remember that filename extensions are just a convention. The MIME type depends on the file's contents, not its name. So if you have an expected list, its trivial to just create an associative array or something to map them. – Michael Berkowski Feb 13 '13 at 02:11
  • Michael, I suppose that could work. I was just hoping there might be a quick built in function I missed somewhere :) – K. Barresi Feb 13 '13 at 02:15
  • You want it to give you back one extension? It isn't a one-to-one mapping. A nine type can have many different extensions that are associated with it. – user2049150 Feb 13 '13 at 02:17

1 Answers1

-1

Something like this?:

<?php

function MIME($mime)
{
  $list = explode("/",$mime);

  return ".".$list[1];
}


$my_mime = "image/jpeg";

$type = MIME($my_mime);

echo $type;

?>

You can improve my code if you want.....i hpe it helps ;) .....saludos

Hackerman
  • 12,139
  • 2
  • 34
  • 45
  • 2
    What about this: `application/vnd.openxmlformats-officedocument.wordprocessingml.document`? `myDocx.vnd.openxmlformats-officedocument.wordprocessingml.document`? – Daniel Cheung Jul 27 '16 at 16:04