mime_content_type() returning 'text/plain' for png

hiya, me again!

I was expecting ‘image/png’ not ‘text/plain’ from this piece of code:

[code:1]$mime_check = mime_content_type(‘a_png_file.png’);
var_dump($mime_check);[/code:1]

I’m using Virtualmin Pro with no bleeding edge stuff, so mime_content_type() is the correct function to use, even though php.net says it is deprecated. The function works correctly with jpeg and gif files - it’s just png files giving me the headache. Any ideas?

If you log into the command line, and type:

file a_png_file.png

What does it show?
-Eric

file new_png_Toucan.png new_png_Toucan.png: PNG image data, 1024 x 768, 8-bit/color RGB, non-interlaced

Hmm, okay.

Well, I’m not sure why it’s not working :slight_smile:

But, looking at the php.net site, it doesn’t just suggest it’s deprecated, it also suggests what you can use in it’s place – the PECL fileinfo extension:

http://us.php.net/manual/en/function.mime-content-type.php

You might have some more luck if you install that and give it a shot.
-Eric

I already read about that:

User Contributed Notes Fileinfo Functions aidan at php dot net 20-Dec-2008 09:09 As of PHP 5.3, Fileinfo is shipped with the main distribution and enabled by default. The extension is no longer maintained in PECL. abdo 18-Oct-2008 09:32 Add extension=fileinfo.so in your php.ini file

or (extension=/path/to/fileinfo.so)
Philip Snyder
05-Sep-2008 10:40
This module needs some serious work. I’m a well versed linux guy and it took me almost an hour of hard research & trying this and that before I could actually get this to work. mime_content_type() is supposed to be deprecated, but if this is the replacement, stick with mime_content_type() as long as you can!..

I’m using PHP 5.1.6 so I came here wondering if maybe I ought to take a gamble with bleeding edge stuff, the latest version of PHP… you guys usually recommend against bleeding edge… I’m still too much a noobie to know what to do… though my server is only a test server… shall I gamble with bleeding edge? Looks like it’ll be the only way to get fileinfo according to what they’re saying over at php.net

Well, I wouldn’t give up on “fileinfo” yet.

It’s hard for me to believe that installing a PECL module could be anywhere near as involved as compiling and maintaining a bleeding edge PHP version :slight_smile:

Plus, the person who made that comment on the PHP.net site didn’t have the awesome folks who hang out in the forum here available to help them out :wink:

Try logging into your server as root, and type:

pecl install Fileinfo

It’s possible that you’re missing a dependency or two – if that fails, look at the error, and see if you can determine if something is missing. If it is, no problem, we can just install it.

If you aren’t sure what to install, pop a message on here so we can help out.

After you install it, you’ll have to add “extension=fileinfo.so” to your php.ini as they mention above.
-Eric

It's hard for me to believe that installing a PECL module could be anywhere near as involved as compiling and maintaining a bleeding edge PHP version :-)

I suspect he’s referring to our bleeding edge package repo. Which is a little bit more sane. We’re running our bleeding edge package on the new website, and haven’t had any problems with it so far.

But, you might check to see if you have libpng installed. Maybe PHP will only check for png files if it has the library for it (but maybe it has to be built against it, and maybe that’s not the source of trouble).

I dunno. None of us here are PHP experts, you know? (Seriously, you probably know PHP better than any of us. I’m getting more comfortable with it, now that I’m doing so much Drupal development, but I still wouldn’t even call myself an informed amateur.)

Bingo! I followed Eric’s advice: pecl install Fileinfo

everything went smoothly. I rewrote my script using the fileinfo functions instead of mime_content_type(), and got the result I was looking for: ‘image/png’ enabling me to check MIME types of uploaded files (thus preventing script kiddies uploading text files disguised as image files).

Thanks for the help :slight_smile: