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!
Comment | File | Size | Author |
---|---|---|---|
#13 | 710638-mimedetect-13.patch | 930 bytes | davidwatson |
#3 | roundie.png | 55.39 KB | davidwatson |
Comments
Comment #1
Weka CreditAttribution: Weka commentedHi 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.
Comment #2
davidwatson CreditAttribution: davidwatson commentedThe 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.
Comment #3
davidwatson CreditAttribution: davidwatson commentedApologies for the double-post; forgot to attach the png if anyone wants to give it a shot.
Comment #4
Weka CreditAttribution: Weka commentedSorry for the delay.
May have something to do with your server config and Fileinfo. I was able to upload your file without any issues.
Comment #5
davidwatson CreditAttribution: davidwatson commentedNo 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?
Comment #6
davidwatson CreditAttribution: davidwatson commentedNo 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?
Comment #7
3rdLOF CreditAttribution: 3rdLOF commentedSorry 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?
Comment #8
davidwatson CreditAttribution: davidwatson commentedWell, 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.
Comment #9
3rdLOF CreditAttribution: 3rdLOF commentedWell, 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
Comment #10
drewish CreditAttribution: drewish commentedThere's no patch. Anyone mind rolling one?
Comment #11
Fa-sum CreditAttribution: Fa-sum commentedI'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.
Comment #12
davidwatson CreditAttribution: davidwatson commented!
How embarassing... I'll get the patch up here later today.
Comment #13
davidwatson CreditAttribution: davidwatson commentedHere is the patch that I apparently didn't submit the first time around. Tested in PHP 5.3.x.
Comment #14
rjbrown99 CreditAttribution: rjbrown99 commentedWorks 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.
Comment #15
girthmaul CreditAttribution: girthmaul commented#13 patch works great on centos 5 + php 5.3. Thank you!
Comment #16
rjbrown99 CreditAttribution: rjbrown99 commentedLet's declare victory! Thanks c.ex.
Comment #17
drewish CreditAttribution: drewish commentedGreat, committed to HEAD and DRUPAL-6--1.