Posted By

jatkins on 04/27/11


Tagged

file type humanize parser mime


Versions (?)

Humanize MIME type


 / Published in: PHP
 

Released into the public domain. Amend $file_types as you wish to add more types.

  1. global $file_types;
  2. $file_types = array('image/jpeg' => 'JPEG Image', 'image/png' => 'PNG Image', 'image/gif' => 'GIF Image', 'text/plain' => 'Text Document', 'text/html' => 'HTML Document', 'text/javascript' => 'JavaScript File', 'text/css' => 'CSS Stylesheet');
  3. function humanize_file_type($mime_type) {
  4. $mime_type = preg_replace('/[^A-Za-z\/]/', '', $mime_type);
  5. if(isset($GLOBALS['file_types'][$mime_type]))
  6. return $GLOBALS['file_types'][$mime_type];
  7. else {
  8. $slash_position = strpos($mime_type, '/');
  9. return $slash_position === false ? $mime_type : strtoupper(substr($mime_type, $slash_position + 1)) . " File";
  10. }
  11. }

Report this snippet  

You need to login to post a comment.