Get mimetype for a file

Published 17 February, 2007 in Programming - 0 Comments

Sometimes you need/want to find out what mimetype a particular file has. If you have access to the PEAR “Fileinfo Functions” you can use them, but if you don’t here’s a simple solution that might be helpful.

NOTE! This is not as reliable as the Fileinfo Functions but it works in most cases.

In most GNU/Linux systems (if not all) there’s a file called mime.types (in /etc/) which is mapping of mimetype -> extension. The file looks like this:

6 lines of Plain text
  1. application/x-gnumeric gnumeric
  2. application/x-go-sgf sgf
  3. application/x-graphing-calculator gcf
  4. application/x-gtar gtar tgz taz
  5. application/x-hdf hdf
  6. application/x-httpd-php phtml pht php

Quite obvious you could build an array from this file where the mimetype could be the key and the extension(s) the value as an array:

3 lines of PHP
  1. $_MIMETYPES = array(
  2. “mime/type” => array(“ext1”, “ext2”, “ext3”)
  3. );

When we’ve got this array populated we can easily look up a mimetype for an extention (and vice versa) by these two functions:

26 lines of PHP
  1. /**
  2. * Find mimetype for extension
  3. * @global array $_MIMETYPES
  4. * @param string $ext
  5. * @return string|bool
  6. */
  7. function find_mimetype($ext)
  8. {
  9. global $_MIMETYPES;
  10. foreach ($_MIMETYPES as $mime => $exts)
  11. if (in_array($ext, $exts))
  12. return $mime;
  13. return false;
  14. }
  15. /**
  16. * Get extension(s) for a given mimetype
  17. * @global array $_MIMETYPES
  18. * @param string $mimetype
  19. * @return array
  20. */
  21. function findextensionfor_mimetype($mimetype)
  22. {
  23. global $_MIMETYPES;
  24. return isset($MIMETYPES[$mimetype]) ? $MIMETYPES[$mimetype] : array();
  25. }

The script to convert the mime.types database into an array an auto generate the two functions above looks like this (note that this PHP script is intended to run as a command line (cli) script, thus the shebang (#!/usr/bin/php), since we don’t need to do this conversion run time but only once):

86 lines of PHP
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * mimetyper.php
  5. * Turn the /etc/mime.types database into a PHP array
  6. * @author Pontus Ostlund <spam@poppa.se>
  7. */
  8. //! Mimetype file
  9. $mime_db = ‘/etc/mime.types’;
  10. //! Resulting file for the PHP array
  11. $php_file = ‘mimetypes.php’;
  12. fileexists($mimedb) or die(“No mimetype database ($mime_db) found\\n”);
  13. $lines = @file($mimedb) or die(“Couldn’t read $mimedb”);
  14. $fh = @fopen($phpfile, “w+”) or die(“Couldn’t create $phpfile”);
  15. $date = strftime(“%A %B %d %Y”, time());
  16. //! Write the PHP header to the file.
  17. @fwrite($fh,
  18. “<?php
  19. /
  20. * Find mimetype for file extension
  21. * Generated $date by “ . basename(<strong>FILE</strong>) .
  22. */
  23. $_MIMETYPES = array(n”
  24. );
  25. //! Loop through the lines array
  26. while ($line = array_shift($lines))
  27. {
  28. //! Match a comment or a blank line
  29. if ($line{0} == “#” || $line{0} == “\\n”)
  30. continue;
  31. //! A line looks like:
  32. //! mime.type extention[ extension[ extension]]
  33. //! so lets grab the mimetype and extensions
  34. preg_match(‘/^(.?)\\s+(.?)$/’, trim($line), $match);
  35. list(, $mime, $ext) = $match;
  36. if (empty($mime)) continue;
  37. //! This converts “ext ext ext” to “ext’,’ext’,’ext”
  38. $ext = join(“‘, ‘”, explode(‘ ‘, $ext));
  39. //! And lets write the new array index to the file
  40. fwrite($fh, sprintf(
  41. “\\t’%s’ => array(‘%s’)%s\\n”,
  42. $mime, $ext, (empty($lines) ? “” : “,”))
  43. );
  44. }
  45. //! Close the array, add a nifty search function, and close the PHP block
  46. //! and we’re done!
  47. fwrite($fh, “);
  48. /**
  49. * Find mimetype for extension
  50. * @global array $_MIMETYPES
  51. * @param string $ext
  52. * @return string|bool
  53. */
  54. function find_mimetype($ext)
  55. {
  56. global $_MIMETYPES;
  57. foreach ($_MIMETYPES as $mime => $exts)
  58. if (in_array($ext, $exts))
  59. return $mime;

  60. return false;
  61. }
  62. /**
  63. * Get extension(s) for a given mimetype
  64. * @global array $_MIMETYPES
  65. * @param string $mimetype
  66. * @return array
  67. */
  68. function findextensionfor_mimetype($mimetype)
  69. {
  70. global $_MIMETYPES;
  71. return isset($MIMETYPES[$mimetype]) ? $MIMETYPES[$mimetype] : array();
  72. }
  73. ?>”);
  74. fclose($fh);
  75. ?>

And this should result in a file called mimetypes.php that you just can include in you PHP scripts.

Download the scripts 17:31, Sat 17 October 2009 :: 4.5 kB