I want to get the MIME type from a filename using C.
Is there a way to do this without using a textfile containing MIME types and file extensions (i.e. Apache's file mime.types
)?
Maybe there is a function to get the MIME type using the filename? I rather not use the file extension if I don't have to.
If there was a way to do it, Apache wouldn't need its mime.types
file!
The table has to be somewhere. It's either in a separate file which is parsed by your code, or it's hard coded into your software. The former is clearer the better solution...
It's also possible to guess at the MIME type of a file by examining the content of the file, i.e. header fields, data structures, etc. This is the approach used by the file(1)
program and also by Apache's mod_mime_magic
. In both cases they still use a separate text file to store the lookup rules rather than have any details hard-coded in the program itself.