Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know how to do this:
$file_data = # ascii or binary data $mime_type = get_mime_type($file_data);
Why do I want to do this? I have binary and text data stored in a database. I don't have the file extension. I need to send a correct http header. Anyone got any ideas?

Replies are listed 'Best First'.
Re: getting mime type from file content
by Moonie (Friar) on Nov 30, 2001 at 03:26 UTC
    You may want to check out File::MMagic. I'm not sure if that'll work.. arturo's suggestion is a good idea.

    - Moon
      Thank you - works perfect.
Re: getting mime type from file content
by arturo (Vicar) on Nov 30, 2001 at 03:18 UTC

    Instead of implementing a potentially expensive and in all likelihood unreliable method of figuring out which is which, why not store the MIME type in the database?

    HTH

      Shouldn't be expensive. I would be happy to just get the output from the unix file command. That would be enough to create a mime type. The file command is fast enough for me. It's too bad I can't do this with the file command `echo hello | file`