Get mimetype for a file
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:
- application/x-gnumeric gnumeric
- application/x-go-sgf sgf
- application/x-graphing-calculator gcf
- application/x-gtar gtar tgz taz
- application/x-hdf hdf
- 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:
- $_MIMETYPES = array(
- "mime/type" => array("ext1", "ext2", "ext3")
- );
When we’ve got this array populated we can easily look up a mimetype for an extention (and vice versa) by these two functions:
- /**
- * Find mimetype for extension
- * @global array $_MIMETYPES
- * @param string $ext
- * @return string|bool
- */
- function find_mimetype($ext)
- {
- global $_MIMETYPES;
- foreach ($_MIMETYPES as $mime => $exts)
- if (in_array($ext, $exts))
- return $mime;
- return false;
- }
- /**
- * Get extension(s) for a given mimetype
- * @global array $_MIMETYPES
- * @param string $mimetype
- * @return array
- */
- function findextensionfor_mimetype($mimetype)
- {
- global $_MIMETYPES;
- return isset($MIMETYPES[$mimetype]) ? $MIMETYPES[$mimetype] : array();
- }
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):
- #!/usr/bin/php
- <?php
- /**
- * mimetyper.php
- * Turn the /etc/mime.types database into a PHP array
- * @author Pontus Ostlund <spam@poppa.se>
- */
- //! Mimetype file
- $mime_db = '/etc/mime.types';
- //! Resulting file for the PHP array
- $php_file = 'mimetypes.php';
- fileexists($mimedb) or die("No mimetype database ($mime_db) found\\n");
- $lines = @file($mimedb) or die("Couldn't read $mimedb");
- $fh = @fopen($phpfile, "w+") or die("Couldn't create $phpfile");
- $date = strftime("%A %B %d %Y", time());
- //! Write the PHP header to the file.
- @fwrite($fh,
- "<?php
- /
- * Find mimetype for file extension
- * Generated $date by " . basename(<strong>FILE</strong>) . "
- */
- $_MIMETYPES = array(n"
- );
- //! Loop through the lines array
- while ($line = array_shift($lines))
- {
- //! Match a comment or a blank line
- if ($line{0} == "#" || $line{0} == "\\n")
- continue;
- //! A line looks like:
- //! mime.type extention[ extension[ extension]]
- //! so lets grab the mimetype and extensions
- preg_match('/^(.?)\\s+(.?)$/', trim($line), $match);
- list(, $mime, $ext) = $match;
- if (empty($mime)) continue;
- //! This converts "ext ext ext" to "ext','ext','ext"
- $ext = join("', '", explode(' ', $ext));
- //! And lets write the new array index to the file
- fwrite($fh, sprintf(
- "\\t'%s' => array('%s')%s\\n",
- $mime, $ext, (empty($lines) ? "" : ","))
- );
- }
- //! Close the array, add a nifty search function, and close the PHP block
- //! and we're done!
- fwrite($fh, ");
- /**
- * Find mimetype for extension
- * @global array $_MIMETYPES
- * @param string $ext
- * @return string|bool
- */
- function find_mimetype($ext)
- {
- global $_MIMETYPES;
- foreach ($_MIMETYPES as $mime => $exts)
- if (in_array($ext, $exts))
- return $mime;
- return false;
- }
- /**
- * Get extension(s) for a given mimetype
- * @global array $_MIMETYPES
- * @param string $mimetype
- * @return array
- */
- function findextensionfor_mimetype($mimetype)
- {
- global $_MIMETYPES;
- return isset($MIMETYPES[$mimetype]) ? $MIMETYPES[$mimetype] : array();
- }
- ?>");
- fclose($fh);
- ?>
And this should result in a file called mimetypes.php that you just can include in you PHP scripts.