fileinfo and mime types I've never heard of

偶尔善良 提交于 2019-12-05 14:29:14

fileinfo is a "best guess". It analyzes only a portion of the file in order to try to figure out what type the file is, and as such it can be fooled easily enough. Perhaps your file starts with a Pascal comment or keyword such as Project or Unit.

Fileinfo is not using the extension of the file to determine which mime-type it is, but (quoting) :

The functions in this module try to guess the content type and encoding of a file by looking for certain magic byte sequences at specific positions within the file.

The idea being that the name à of the file, and its extension, are provided by the users (especially in a case such as yours, where the files are being uploaded by users), and, as such, are less "sure" than the content of the file itself.


Maybe a solution could be to not check on the whole mime-type returned by fileinfo, but to only use the first part of it -- at least in some cases ?

For instance, maybe you could accept all mimetype that are in the text/* and image/* families, and refuse all those look like application/*, except for application/pdf ?
(Just an example -- but you see the point)

Andy Jackson

I have found that, as of at least version 5.03, the 'file' command can in some circumstances mis-identify a plain text file as a Pascal source file, simply because it contains the word 'program' or 'record'. At least that's how it looks having examined the source (src/names.h). I believe the php fileinfo command uses the same 'magic' engine, so I suspect this is the cause of the problem. If/when I am accepted on the file mailing list, I will notify the maintainers of this issue.

[UPDATE] I asked the question, but got little in the way of a response. Having investigated this issue a bit more throughly, it turns out that identifying text formats is, in general, really difficult. If you get a 'text/*' MIME type back from file, you might want to consider ignoring the result and assuming the resource is just 'text/plain', unless the false negatives (text/html maybe) will cause you difficulties.

Sinan

There is a chart that shows a list of common MIME types and their corresponding extensions. Here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!