Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created November 17, 2009 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipelavinz/237123 to your computer and use it in GitHub Desktop.
Save felipelavinz/237123 to your computer and use it in GitHub Desktop.
Helper function to determine the document type
<?php
/**
* Get document type based on the mime type
* (Loosely) based on wp_ext2type() on WordPress
*
* @param Object|string $attach WordPress object for an attachment or mime type
* @param boolean $echo Whether to echo (true) or return (false) the out
* @return string Three-letter file type, based on MIME-Type groups, or 'file' if there's no match
*/
function the_doc_type($attach, $echo = TRUE){
global $post; $out;
$mime = ( is_object($attach) ) ? $attach->post_mime_type : $attach;
$types2mime = array(
'pdf' => array('application/pdf', 'application/x-pdf', 'application/acrobat', 'applications/vnd.pdf', 'text/pdf', 'text/x-pdf'),
'text' => array('application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.oasis.opendocument.text'),
'presentation' => array('application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'application/vnd.oasis.opendocument.presentation'),
'spreadsheet' => array('application/vnd.ms-excel', 'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel.sheet.macroEnabled.12'),
'audio' => array('audio/mpeg', 'audio/x-vorbis+ogg', 'audio/x-ms-wma', 'audio/aac', 'audio/mp4', 'audio/wav', 'audio/wave', 'audio/x-wav'),
'video' => array('video/x-ms-wmv', 'video/asf', 'video/avi', 'video/x-msvideo', 'video/mp4v-es', 'video/quicktime', 'video/x-quicktime')
);
$i=0; foreach($types2mime as $type => $mimes) {
if( in_array($mime, $mimes) ){
$out .= $type;
}
++$i;
}
$out = ( empty($out) ) ? 'file': $out;
if($echo) echo $out; else return $out;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment