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

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.
[toc title=”Table of Contents”]

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.

  • Tony

    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.

    • I’m not sure. Never thought about doing it. I’ll look into it.

  • Rob

    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 ?

    • 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.

  • Rajiv

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

    Great job !

  • Looks like a solution, but I don’t know where to put the fix. Could find the file, don’t know what the first few lines of the fix mean. Never worked with such a fix.
    I couldn’t find the file mod_mime either…
    Mediawiki version 1.18.0.
    Can you help me a bit further? Thanks in advance!
    -klb-

    • Well, this post was written ages ago. It may not still work the same way…

  • The newer location to upload custom .png icon files for specific [blank] MIME types (on MediaWiki 1.24.x) is located at: /resources/assets/file-type-icons