Allowing doc/ppt/xls uploads to MediaWiki and getting proper MIME types back

MediaWiki

The (terrible) doc/ppt/xls Microsoft Office document formats all have the same MIME type. This causes a lot of grief when using MediaWiki, as MediaWiki checks MIME types against file extensions for security purposes. In this article I’ll describe how to allow uploading for these file types, and how to get around the “The file is corrupt or has an incorrect extension” problem.

Allow doc/ppt/xls to be uploaded

Add the following lines to your LocalSettings.php to allow these formats:

$wgFileExtensions[] = 'doc';
$wgFileExtensions[] = 'ppt';
$wgFileExtensions[] = 'xls';

Fix the “The file is corrupt or has an incorrect extension” problem

MediaWiki is going to see all of these formats as “application/msword”, as they all have the same MIME type. So, we need to tell MediaWiki to handle them as such. Change the following line in “includes/mime.types” from:

application/msword doc

to:

application/msword doc ppt xls

Now MediaWiki will treat all these formats as Microsoft Word documents. Unfortunately, that also means that when users open these files from your wiki, they’ll open in Word.

Display the proper MIME types back to your users

Fixing the displayed MIME type when using img_auth.php

If you have a locked down wiki, you are likely using img_auth.php instead of your web server to send files back. We need to do some code modification to make this work properly.

Fixing “includes/MimeMagic.php” in MediaWiki 1.12+

Apply the following patch:

--- MimeMagic.php.old   2009-04-21 13:03:40.000000000 -0500
+++ MimeMagic.php       2009-04-21 12:26:15.000000000 -0500
@@ -334,6 +334,12 @@
                $m = trim( $m );
                $m = preg_replace( '/s.*$/', '', $m );
+               if ( $ext == 'ppt' ) {
+                       $m = 'application/vnd.ms-powerpoint';
+               } else if ( $ext == 'xls' ) {
+                       $m = 'application/vnd.ms-excel';
+               }
+
                return $m;
        }

Fixing “includes/MimeMagic.php” in MediaWiki 1.11 and below

Apply the following patch:

--- MimeMagic.php.old   2009-04-21 13:07:49.000000000 -0500
+++ MimeMagic.php       2009-04-21 13:08:58.000000000 -0500
@@ -497,6 +497,19 @@

                }

+               # New excel files have the same magic number as msword files
+               if ( $mime == "application/msword" ) {
+                       $i = strrpos( $file, '.' );
+                       $e = strtolower( $i ? substr( $file, $i + 1 ) : '' );
+
+                       if ( $e == "xls" ) {
+                               $mime= "application/vnd.ms-excel";
+                       }
+                       if ( $e == "ppt" ) {
+                               $mime= "application/vnd.ms-powerpoint";
+                       }
+               }
+
                if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
                        $mime = $this->mMimeTypeAliases[$mime];
                }

The above patches tell MediaWiki to send the proper MIME types based upon the type of extension the file has, not the one it detects.

Fixing the displayed MIME type when using Apache

Using mod_mime, you can tell Apache to send out the proper MIME types based on extension:

AddType application/vnd.ms-excel .xls
AddType application/vnd.ms-powerpoint .ppt

Testing

Now upload test doc, ppt, and xls files. Ensure that you can upload them, and that when you download them, that they open in the correct applications.

5 Comments

5 Responses to “Allowing doc/ppt/xls uploads to MediaWiki and getting proper MIME types back”

  1. Tony says:

    I am going to try this out. How do you then get thumbnails that represent the ms office mimetypes? Currently the thumbnail is a generic “blank file” png.

  2. Rob says:

    found that you can also add to the /skins/common/images/icons folder files called fileicon-ppt.png and fileicon-xls.png so that suitable icons appear in the image gallery page…
    only thing i cant get to work is that when you click through to the image page… theres no icon there, instead it pops back to application/msword filetype and has no icon…
    things like pdf work fine and have both icon on the gallery and image sumamry pages…

    any ideas how to get correct icons to propogate to the individual items summary page ?

    • Ryan Lane says:

      Nope. I’ve never done that before. Didn’t even know you could add the file icons to that directory. Thanks for the info. I’ll let you know if I find anything out.

  3. Rajiv says:

    Thanks! I tried this out and it has solved my problem.

    Great job !

Leave a Comment