When trying to upload an image to Imagefield, I was met with the following error running the latest MimeDetect on Drupal 6.15 on PHP 5.3 and CentOS 5.3:

The selected file foo.png could not be uploaded. The file contents (application/octet-stream; charset=binary) do not match its extension (png).

After confirming the MIME types of multiple images that returned the same error to make sure the problem wasn't on my end, I traced the bug back to MimeDetect. Although I'm not sure why Fileinfo wouldn't identify the file more specifically, I did notice that the fallback at the end of mimedetect_mime() assumes that the charset would be trimmed already - something that isn't done in the Fileinfo block.

The fix (attached) is trivial: move the charset trimming outside of the block that follows, to cover both cases. It was tested using both valid and invalid file inputs, and behaves as it should in my environment - in my case, allowing/denying image uploads as appropriate.

Review and feedback welcome!

CommentFileSizeAuthor
#13 710638-mimedetect-13.patch930 bytesdavidwatson
#3 roundie.png55.39 KBdavidwatson
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Weka’s picture

Hi c.ex, do you get the same problem with MimeDetect 6.x-1.2 ? I am able to upload all png jpg and gif files without any errors?

If you attach the culprit png file here, I would give it a try and see if I can replicate the problem.

davidwatson’s picture

The root issue on my end doesn't seem to be with MimeDetect so much as Fileinfo - sorry, should have been more clear on that. Running file returns:

roundie.png: PNG image data, 553 x 257, 8-bit colormap, non-interlaced

I tested on a number of files in different formats, with similar results. The issue in MimeDetect is that it assumes that Fileinfo will always return a sane value without a character set... which apparently isn't always the case. I'm still looking into the reasons why that may be; if I find it I'll be sure to follow up here.

EDIT: To clarify, same issue in both dev and 6.x-1.2.

davidwatson’s picture

FileSize
55.39 KB

Apologies for the double-post; forgot to attach the png if anyone wants to give it a shot.

Weka’s picture

Sorry for the delay.
May have something to do with your server config and Fileinfo. I was able to upload your file without any issues.

davidwatson’s picture

No trouble at all!

I thought as much, but I imagine the error case (strange as it may be) should still be handled correctly in the module, right?

davidwatson’s picture

No trouble at all!

I thought as much, but I imagine the error case (strange as it may be) should still be handled correctly in the module, right?

3rdLOF’s picture

Sorry to reopen this, but I am having this EXACT same issue on an almost=identical framework.

So what was exactly the issue? Was it the fileinfo configuration, the Mime Detect plugin or a combination of both?

if it was the fileinfo at the server...What was the problem?

davidwatson’s picture

Well, technically the issue is still open - even if Fileinfo is at fault (and I still don't understand why), the corner-case where it isn't working should be detected and handled correctly.

Sadly, I haven't been able to find anything more; only that Fileinfo itself is not returning expected values.

3rdLOF’s picture

Well, I tracked and applied your suggestion by moving the trim out of the conditional and the issue went away.....

Is it fixed? Well, I can upload now.

But other things are happening to other modules that MAY or MAY NOT be related to this ( I am in one of those multi-module bug trifecta from hell). If I find out whatever issues I am having are related to this I will report back. If not then, at least for me, what you suggested is working.

Thanks

drewish’s picture

Status: Needs review » Active

There's no patch. Anyone mind rolling one?

Fa-sum’s picture

I've this problem too.
With PHP 5.3, when I try to upload an image with imagefield I have this error:
The file contents (image/jpeg; charset=binary) do not match its extension (jpg).

I subscribe and hope that someone will be able to solve.
Many thanks in advance.

davidwatson’s picture

!

How embarassing... I'll get the patch up here later today.

davidwatson’s picture

Status: Active » Needs review
FileSize
930 bytes

Here is the patch that I apparently didn't submit the first time around. Tested in PHP 5.3.x.

rjbrown99’s picture

Works for me. I could not upload files without this patch on Fedora 11, PHP 5.2. I also tested on Fedora 12.

How many positive votes do we need for RTBC status? Count me as a +1.

girthmaul’s picture

#13 patch works great on centos 5 + php 5.3. Thank you!

rjbrown99’s picture

Status: Needs review » Reviewed & tested by the community

Let's declare victory! Thanks c.ex.

drewish’s picture

Status: Reviewed & tested by the community » Fixed

Great, committed to HEAD and DRUPAL-6--1.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.