How do I get the MIME type of a file basing on its content and not its extension? I want to get the exact MIME type basing on the file content, not its extension.
I have used PHP methods and the API exposed by a module, but they return the MIME type basing on the file extension.

How do I get the MIME type from the file content?
You can easily get the MIME type of a file using mime_content_type() function.
$mime_type = mime_content_type($path_to_the_file);

I've just tested it on my local machine; placed a test.pdf file in my home folder and renamed it giving it no and an incorrect extension. In both cases I got the right MIME type:

$ ls
test.pdf

$ drush eval 'var_dump(mime_content_type(&quot;/home/elin/test.pdf&quot;))'
string(15) &quot;application/pdf&quot;

$ mv test.pdf test
$ drush eval 'var_dump(mime_content_type(&quot;/home/elin/test&quot;))'
string(15) &quot;application/pdf&quot;

$ mv test test.jpg
$ drush eval 'var_dump(mime_content_type(&quot;/home/elin/test.jpg&quot;))'
string(15) &quot;application/pdf&quot;

$ mv test.jpg test.png
$ drush eval 'var_dump(mime_content_type(&quot;/home/elin/test.png&quot;))'
string(15) &quot;application/pdf&quot;