From: khaines@... Date: 2006-08-11T16:01:12+09:00 Subject: Re: Getting a files mime-type On Fri, 11 Aug 2006, Daniel Greig wrote: > Can anyone tell me if there is a simple way to get the mime-type of a > file? > (I've seen the Mime::Type library, but haven't figured out how to use > it). > I'm trying to check the type before processing a bunch of files on disk > (and ignoring files that I'm not interested in. Is there a better way > than using the file extension? MIME::Types.type_for(filename) Using a file extension is fast. It's what MIME::Types does. It obviously doesn't work if the extension doesn't match the file type, though. The alternative is to use a heuristic that identifies distinct characteristics of different file types in order to guess at the type. This can identify the type of a file when the extension doesn't match the type, but it isn't necessarily 100% certain, either. If you have Apache, look in the conf directory for a 'magic' file. mod_mime_magic uses that to perform type guessing. I am unaware of a mime magic library for Ruby at this time, but if anyone knows of one, I'd love to be pointed towards it. Kirk Haines