Created
November 17, 2009 18:30
-
-
Save felipelavinz/237123 to your computer and use it in GitHub Desktop.
Helper function to determine the document type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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