How to determine file type from data stream

jdavidb on 2008-08-11T13:46:36

I'd like to use MIME::Lite to take arbitrary data as input and send an email with that data as an attachment. I presume that to do this I need to get the MIME type correctly set. So I'm looking for something that can examine that stream of arbitrary, presumably binary, data, and then spit out the MIME type, or else something that I can use to derive the MIME type.

Basically, I'm looking for the "file" command in Perl. I was just remarking with a co-worker the other day how "file"'s database is an incredible resource and how that utility really does an amazing job figuring out what formats things are. It takes a hard job that can never been done perfectly, and does it intelligently. (Actually, that's my definition of artificial intelligence. Maybe "file" is sentient.)

I thought of looking to see if Perl Power Tools has a file command, but ppt.perl.org seems to be unreachable from my position in time and space, and I'm not really sure I want to download the entire distribution and pore through it, especially since whatever I find probably won't be a library, anyway. I did find a module called MIME::Types, updated as recently as May of this year, but it doesn't really do this job; it will give me a MIME type based on a file extension or based on a filename, which does me no good since STDIN doesn't come with a filename. :) If I had a filename, I most likely wouldn't need the module I'm looking for. The docs for MIME::Types mentions a File::TypeInfo module, but that doesn't seem to exist on CPAN at this point in time.

Even if I need to do this some other way, or don't need to do this at all to achieve my goal, I'm still curious if anything like this exists.


CPAN to the rescue

acme on 2008-08-11T13:59:24

file works via magic numbers. Use File::Type or File::MMagic.

Re:CPAN to the rescue

jdavidb on 2008-08-11T18:25:41

Thank you!