[dev] mime recognition - extension based

jason kawaja kawaja@ece.ufl.edu
Wed, 10 Jul 2002 16:45:58 -0400 (EDT)


greetings.

is the some consensus about determining mime type based on filename
extension?

the follow code placed in the the VIEW_ATTACH case statement of
imp/view.php does the trick for word, excel and ppt files :

     $fname = MIME::decode($mime->name);
     $tmp   = explode(".", $fname);
     if (sizeof($tmp) == 2) {
      $ext   = $tmp[1];
      switch ($ext) {
       case 'doc':
        $mime_type = 'application/msword';
        break;
       case 'xls':
        $mime_type = 'application/msexcel';
        break;
       case 'ppt':
        $mime_type = 'application/mspowerpoint';
        break;
       default:
        $mime_type = $mime->TYPE . '/' . $mime->subtype;
      }
     }
     else {
      $mime_type = $mime->TYPE . '/' . $mime->subtype;
     }

     /* If this is a text/html part, then we need to scan for CIDs
      * embedded in it in order to display images that might be
      * inlined in other parts of the message. */
     if ($mime_type == 'text/html' ) {
         include_once HORDE_BASE . '/lib/MIME/Structure.php';
         MIME_Structure::parse(imap_fetchstructure($imp['stream'],
$index, FT_UID), $index, $MimeID);
     }

     $ob = new MIME_Part($mime_type, IMP::getDecodedPart($mime),
$mime->charset);
     if (isset($mime->filename)) {
         $ob->setName($mime->filename);
     }
     $viewer = &MIME_Viewer::factory($ob);

     header('Content-Type: ' . $viewer->getType() . '; charset=' .
$viewer->getCharset($ob));
     header('Content-Disposition: inline; filename=' .
MIME::decode($mime->name));
     print $viewer->render($mime);
     exit;
     break;

am wondering why something similar has not been added in yet.

/* Regards,
   Jason Kawaja, UF-ECE Sys Admin */