Fixing broken PNG uploads with MediaWiki

Posted on June 18th, 2007 by

Our Technology Services wiki is powered by MediaWiki—the same software that powers Wikipedia. So far, most things have worked relatively well for us. Even a few upgrades have gone pretty smoothly.

The problem

However, when we moved to a new server last week, we were stuck with a puzzling problem. Any images in PNG format that were uploaded to our wiki weren’t being properly resized for thumbnails. After some investigation, it looked like MediaWiki wasn’t recognizing the newly uploaded PNG files as images—it thought their MIME types were all text/plain instead of image/png.

We finally pinpointed the source of the problem today. Apparently, MediaWiki uses a PHP extension called mime magic to identify MIME types (which appears to be deprecated in favor of the Fileinfo extension). Upon running a quick phpinfo(), we found the location of the “magic file” and cracked it open for a peek. We scrolled through the code and noticed that there were definitions for all sorts of other file types, including GIFs and JPEGs, but nothing for PNG files.

The fix

So, based on the definition for GIF images in our magic file, we added the following lines to the images section of our magic file:

# PNG images
1       string          PNG             image/png

Saved, restarted apache, tried the upload again, and everything worked like a dream. Hopefully this can help you out if you encounter a similar issue.

 


17 Comments

  1. Erik says:

    Thanks for this quick and clean fix. I was almost going crazy trying to figure out what the problem was with only .pngs.

  2. Joe Lencioni says:

    Erik, I’m glad that you found it helpful. Thanks for leaving a nice comment.

  3. Where do I find this file? Sorry, I barely know PHP, but run a fairly big mediawiki installation, which I’ve just upgraded to 1.10.

  4. Joe Lencioni says:

    @Scott: here’s how to find the location of the magic file. Create a new php page with the following code in it:

    <?php
    phpinfo();
    ?>
    

    Then save that on your web server and visit it. Somewhere in there there should be a section called mime magic. One of those variables contains the location of the magic file.

    Hope that helps!

  5. Eric says:

    You are the savior!!!!!!!!!!!!!!!!!!!!!

    Thanks soooooooo much for your help!!!! I thought it was Apache’s problem at the beginning, then i added

    0 string PNG image/png

    to the magic configure file…. but it doesn’t work… so i’ve spent hour and hour of work digging down MediaWiki….

    But now… you’ve saved me… Thank you so much.. and google..

    🙂

    Cheers

  6. Dan says:

    Wow. Worked for me too. Nice write up.

  7. mahesh says:

    Great work! Thanks for sharing this.

  8. Sadalwantar says:

    hi,

    i can’t edit my mime.types of the server and have this problem with v1.11 of mediawiki.
    i can not upload PNGs in general! or i deactivate the mime-type check and have only text/plain.
    what can i do now? MW won’t be able to guess the right mime type…

  9. Sadalwantar says:

    so, i have fixed it by myself. i got the idea after eating. ;o)

    for users with the same problem:
    a little hack in a MW file

    FIND

    	$mime = "application/x-php";
    }

    (maybe line 494 and 495)

    AFTER

    // code for PNG check
    if( substr ( $head, 1, 3) == "PNG") {
    	$mime = "image/png";
    }

    this should fix this a little bit.
    i have looked into an PNG and found that the 2nd til 4th byte is “PNG” – so you’ve only look for this string and fine!

    it’s not the best solution and you have to take care if you update your MW.

  10. Joe Lencioni says:

    @Sandalwantar: I’m glad you could get things sorted out. 🙂 Thanks for posting your solution! Is it possible to have you let us know which file you modified?

    i have looked into an PNG and found that the 2nd til 4th byte is “PNG” – so you’ve only look for this string and fine!

    Yes, you are correct. I believe this is exactly what mime magic does. Too bad you can’t edit your magic file.

  11. Luuc says:

    Same problem here, can’t access the magic file either, but when I linked my host to this post, they added the line. As such, thanks for posting problem + fix online ;).

  12. Joe Lencioni says:

    @Luuc: No problem. I’m glad it was helpful and it’s awesome that your host was cool enough to make the change for you.

  13. Paula says:

    You are awesome, and the 3rd hit on google for “png showing as text file mediawiki”, thanks a bunch!

  14. Darryl says:

    Yup fantastic. I knew I had to add something to the ‘magic’ file, but I wasn’t sure what the format was. I read the MimeMagic documentation, but it didn’t give me even a clue.

  15. Sandalwanter suggested editing “a MW file” and pointed us to:

    “FIND

    $mime = “application/x-php”;
    } . . .”

    Someone asked what file that’s found in. It appears to be the /includes/MimeMagic.php file, because I applied Sandalwater’s hack to my /includes/MimeMagic.php file, and it solved my unable-to-upload-only-PNG-files problem. And the code to “Find” was indeed on lines 494 and 495 in my file.

    I chose Sandalwanter’s method because I do not have access to the server, and my hosting service (Servage.net) is just not helpful — ever (argh!). I don’t know if Sandalwanter’s hack is a safety concern — comments on that, anyone?

    What I don’t understand is how widespread this problem is, why the problem exists, and why more MW users aren’t yelling, “Hey, I can’t upload PNGs,” and why there isn’t one iota of help on this topic on MW.org. (I will go there and put something in soon, including a link to this thread. I’m glad I kept searching and searching, because I was just about to give up, but I want to be able to upload PNGs. I just don’t get it.

    MediaWiki: 1.11.1
    PHP: 5.2.3 (apache2handler)
    MySQL: 5.0.51a
    URL: http://www.DishiWiki.com

  16. Liza Johnson says:

    Good Job dude. I faced same problem a year back and one of my consultant guide me but that was a long procedure to fix this error.