You'll have to do a binary read of the first bytes of the file, and check the contents for markers the format is supposed to have.
For instance, a .gif starts with GIF, jpeg has TFIF in the first ten bytes etcetera. You'll have to find a list of file extensions and their marker end code that in...