That depends on what level of complexity you want to involve in your program. Even windows doesn't get all bit-level on you to determine the file type, it simply uses the extension and then maps that extension to an application via the registry. You could develop patterns for certain files or attempt to convert them to base64 and look for certain strings of data to determine the type, for example: the base64 of a jpg typically starts with "/9j/"
here is an example of part of a base64 string for a jpg:
The complete base64 is quite large, and it would be difficult to parse through any further than the first several characters.
I'm not about other files types, but unless the difference is REALLY important, I would just stick with the extension. Alternatively you could check for multiple extensions, like if someone attempted to circumvent a file upload like renaming bad.exe to bad.exe.tmp or bad.tmp. Hope this helps.