Determine File's Type

I'm writing a multitool designed to work solely on images (specifically, JPEG, PNG, and GIF).

Part of the tool is to iterate over the files in a directory and, for each one, verify that if the file's extension is (for example) ".png" that the file itself is, indeed, a PNG (and, obviously, to change the file's name in case of a mismatch).

The iteration and text processing is easy enough. What I need is a means by which I can get the filetype of a given file.

And, ideally, I would want this to be OS-neutral enough for me to compile the program for both Windows and Linux versions.
Read the first few bytes and compare them to the file types magic number http://en.wikipedia.org/wiki/List_of_file_signatures
If you plan this to be anything but a toy...

Maintaining a massive database of every known file type signature (or code to recognize a file without a specific signature) is a herculean task.

If you are focusing on a specific subset of file types (image files, say), your task is manageable, though not possible to be perfect or complete.

JSYK.
Topic archived. No new replies allowed.