How do I get the MIME type of a file basing on its content and not its extension?
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;