Take a look at mime_content_type or Fileinfo. These are built-in PHP commands for determining the type of a file by looking at the contents of the file. Also check the comments on the above two pages, there are some other good suggestions.
Personally I’ve had good luck using something that’s essentially
system("file -bi $uploadedfile"), but I’m not sure if that’s the best method.