views:

202

answers:

1

Where could I find out the content type for a specific file format/extension? For example, I know that an mp3 can be one of the following content types:

'application/mp3', 'application/x-mp3', 'audio/mpeg', 'audio/mp3'

What would be the content type for a zip file? Where would I find out that information for other content types?

I need this for purposes of validation with ruby on rail's paperclip plugin.

Thanks!


Update: I just found this and thought it may be useful to other people

+1  A: 

I know that an mp3 can be one of the following content types

Only audio/mpeg is valid. The rest are bogus and won't generally work anywhere.

What would be the content type for a zip file?

application/zip.

Where would I find out that information for other content types?

The proper list of all registered media types is at IANA. Of course, there are many more types that have not been registered, largely those with the x prefix.

If you want to find out on the fly what your machine thinks a particular extension is using a server-side script, you can find that in an OS-specific place. On Windows, the file extension/type information is stored in the Registry. On desktop Linux, it's in /usr/share/mime. On an Apache web server, you might want to re-use Apache's mime.types table.

for a specific file format/extension?

File extensions are unstandardised, and not a reliable way of checking the type of a resource on the web, including a file upload. File-extension-to-media-type mappings are an OS/installation-specific issue so you can't know what media type a file is from extension on another machine. Furthermore, some operating systems don't even use file extensions to determine type (or only use them as a fallback).

Even looking at the Content-Type supplied with a file upload is of little use, since it's so often wrong due to bad setup at the client side. I guess this is why the bogus types like application/mp3 have crept into the list above, because some time one browser somewhere on a badly-set-up machine sent the wrong type. But browsers can always send any old wrong type... you're just as likely to get a text/plain MP3.

If you need to allow the user to set a definitive content-type for the uploaded resource, you'll have to let them manually pick it. Of course you can use a bit of script hacking to make useful guesses like “if it ends with .jpeg, it's probably a JPEG” and make that selection automatically. But in the general case it is not reliable to guess type from user-submitted file extension.

bobince
wow! thank you for the elaborate answer!
yuval