MIME Type detection function

View: New views
2 Messages — Rating Filter:   Alert me  

MIME Type detection function

by Shahar Evron :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I was going to write a general purpose mime-type detection method. Right
now I use mime_magic to detect uploaded file's MIME type in
Zend_Http_Client but mime_magic is deprecated in favor of the fileinfo
extension.

Since both might or might not be available, and fileinfo is prefered, I
was going to write a wrapper function that would use fileinfo if
available, then mime_magic and if none are available, will do nothing.

I figure that this might be useful for other ZF components, and also for
users, and the most natural place for it is some static method of
Zend_Mime.

Any thoughts or better suggestions?

Shahar.

--
Best Regards,

Shahar Evron  <shahar.e@...>

Senior PHP Specialist         Product Group
Zend Technologies


signature.asc (204 bytes) Download Attachment

Re: [fw-core] MIME Type detection function

by Matthew Ratzloff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Shahar,

I once wrote a (now archived) proposal called Zend_Mime_Magic.  It provided native magic format parsing and was based on libmagic's uncompiled magic format--the format that fileinfo also uses.  I had actually resumed work on it recently but stopped again when the authors of libmagic switched exclusively to a compiled format that is incompatible with my code.  Incidentally, this breaks fileinfo as well, but that's easier to fix since it's a simple wrapper around libmagic.

Still not sure how I want to proceed with that.  The libmagic format has the advantage of being on most systems either from the start or easily through an apt-get or yum install (sudo apt-get install file).  Gnome and KDE use a unified format called Shared MIME-info Database, but it's largely extension based, so that's not acceptable.  Apache's mod_mime_magic magic.mime file descends from an older version of libmagic and is too simplistic.  If I want to continue with the proposal I'll probably have to rewrite the parser for the compiled format.

Anyway, all that said, I think a static method in Zend_Mime wouldn't hurt.  Zend_Mime::getMimeType($filename) could always be modified in the future to use Zend_Mime_Magic if or when it is finished.

-Matt

You should not ever use ext/mime_magic.  It is awful and highly inaccurate.

On Fri, May 30, 2008 at 8:11 AM, Shahar Evron <shahar.e@...> wrote:
Hi,

I was going to write a general purpose mime-type detection method. Right
now I use mime_magic to detect uploaded file's MIME type in
Zend_Http_Client but mime_magic is deprecated in favor of the fileinfo
extension.

Since both might or might not be available, and fileinfo is prefered, I
was going to write a wrapper function that would use fileinfo if
available, then mime_magic and if none are available, will do nothing.

I figure that this might be useful for other ZF components, and also for
users, and the most natural place for it is some static method of
Zend_Mime.

Any thoughts or better suggestions?

Shahar.

--
Best Regards,

Shahar Evron  <shahar.e@...>

Senior PHP Specialist         Product Group
Zend Technologies