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?

2 Answers 2


Suppose you have the file binary source sent in a post request JSON encoded or any encoding method you could get the MIME type like in the following piece of code

$data = base64_decode(trim($_REQUEST['file']));
$finfo = finfo_open();
$mime_type = finfo_buffer($finfo, $data, FILEINFO_MIME_TYPE);
// get file type png. jpg
$ext = $mime_type ? str_replace('image/', '', $mime_type) : 'png';

Hint:Its better to send the file mime type in the request as encoding and decoding may cause not get the MIME type.

Using file_get_contents

$buffer = file_get_contents($url);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$ext = $finfo->buffer($buffer);

More information found here

Save the file using file_save_data to be a managed file and you could then use it as a field in any entity

  • thanks for your response but I have already tried this it's not working, Commented Nov 23, 2015 at 11:17
  • do you get a post request or have the file url get it via file get contents Commented Nov 23, 2015 at 11:19
  • No, i don't have any post request, i want to get mime type in drupal hook_file_validate where i have file object. api.drupal.org/api/drupal/modules!system!system.api.php/… Commented Nov 23, 2015 at 11:33
  • @MohammedGomma +1 for the file_get_contents() example! Commented Aug 13, 2021 at 19:59

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

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

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

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

$ mv test.jpg test.png
$ drush eval 'var_dump(mime_content_type("/home/elin/test.png"))'
string(15) "application/pdf"
  • Please change your file extention pdf to png then Check what type of mime you get Commented Nov 23, 2015 at 16:58
  • @KamalOberoi The same result. Updated my answer with the output.
    – Elin Y.
    Commented Nov 24, 2015 at 8:02
  • Thanks for your reply, but i have used it and not able to get correct mime type.. i have always got application/octect-stream. Commented Dec 1, 2015 at 3:52
  • Which version of PHP are you using? I've tested it on PHP 5.5.9
    – Elin Y.
    Commented Dec 4, 2015 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.