Why getMimeType() is returning wrong value for xlsx file Below is my 2 line code to get the Mime Type of an attachment in a preprocess function 

$file = $node-&gt;get('field_attachment_file')-&gt;entity;
$variables['attach_type'] = $file-&gt;getMimeType();


The $variables['attach_type'] is always getting the value of 'application/pdf' despite the attachment being an Microsoft Excel spreadsheet
The value returned as MIME type of a file is the value returned from the file.mime_type.guesser service which, on a default Drupal installation, uses three classes to guess the MIME type:


The ExtensionMimeTypeGuesser class (a Drupal core class) guesses it from the file extension, but it doesn't have a mapping for the .xlsx files, for which it returns application/octet-stream (See ExtensionMimeTypeGuesser::guess() and ExtensionMimeTypeGuesser::$defaultMapping.)
The FileBinaryMimeTypeGuesser class (a Symfony class) returns the output of file -b --mime &lt;filename&gt; 2&gt;/dev/null
The FileinfoMimeTypeGuesser class (a Symfony class) returns the output of finfo_file()


It is also possible that a module implements hook_file_mimetype_mapping_alter() to change the value returned from the first class, the file used from file or fileinfo_file() contains the wrong values, or another module implements another guesser class that is used before the other classes and returns the wrong MIME type.