12

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.

3 Answers 3

15

I just implemented this for a project on which I'm working. libmagic is what you're looking for. On RHEL/CentOS its provided by file-libs and file-devel. Debian/Ubuntu appears to be libmagic-dev.

http://darwinsys.com/file/

Here's some example code:

#include <stdio.h>
#include <magic.h>

int main(int argc, char **argv){
  const char *mime;
  magic_t magic;

  printf("Getting magic from %s\n", argv[1]);

  magic = magic_open(MAGIC_MIME_TYPE); 
  magic_load(magic, NULL);
  magic_compile(magic, NULL);
  mime = magic_file(magic, argv[1]);

  printf("%s\n", mime);
  magic_close(magic);

  return 0;
}

The code below uses the default magic database /usr/share/misc/magic. Once you get the dev packages installed, the libmagic man page is pretty helpful. I know this is an old question, but I found it on my hunt for the same answer. This was my preferred solution.

1
  • If you are getting segmentation fault (core dumped) with this code on some Linux distributions, that's because the magic database under /usr/share/file/ are in the binary form. remove the line magic_compile(magic, NULL); to prevent compile the magic database twice.
    – user12173053
    Oct 29, 2022 at 8:22
7

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.

2
  • Too bad. But aren't Apache able to find out mimetype without checking file extension? I know some of my friends who types i.e. domain.tld/picture and their Apache's returning a JPG. Is that because the mimetype is in the JPG? Thanks for a quick answer! Jan 8, 2009 at 9:00
  • 3
    the Unix utility "file" makes (very) good guesses at file types by examining headers, data structures, etc. But it uses a separate file (typically /usr/share/file/magic) to store the lookup heuristics! Apache's mod_mime_magic works the same way.
    – Alnitak
    Jan 8, 2009 at 9:15
6

as far as I know, the unix command file outputs the mime string with the option -i:

> file -i main.c
main.c: text/x-c charset=us-ascii
3
  • How could that be used using C ? Jan 8, 2009 at 10:10
  • either by just calling it system("file -i" pszFilename) or looking in the code of file, rebuild the needed parts to parse /usr/share/file/magic
    – lImbus
    Jan 8, 2009 at 10:44
  • Nice, I'll try this as soon as I can reach my UNIX server again. :) Jan 8, 2009 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.