5.x file.inc file_get_mimetype($filename, $mapping = NULL)
6.x file.inc file_get_mimetype($filename, $mapping = NULL)
7.x file.inc file_get_mimetype($uri, $mapping = NULL)

Determines an Internet Media Type or MIME type from a filename.

Parameters

$uri: A string containing the URI, path, or filename.

$mapping: An optional map of extensions to their mimetypes, in the form:

  • 'mimetypes': a list of mimetypes, keyed by an identifier,
  • 'extensions': the mapping itself, an associative array in which the key is the extension (lowercase) and the value is the mimetype identifier. If $mapping is NULL file_mimetype_mapping() is called.

Return value

The internet media type registered for the extension or application/octet-stream for unknown extensions.

See also

file_default_mimetype_mapping()

Related topics

6 calls to file_get_mimetype()
FileMimeTypeTest::testFileMimeTypeDetection in modules/simpletest/tests/file.test
Test mapping of mimetypes from filenames.
file_module_test_file_download in modules/file/tests/file_module_test.module
Implements hook_file_download().
file_save_data in includes/file.inc
Saves a file to the specified destination and creates a database entry.
file_save_upload in includes/file.inc
Saves a file upload to a new location.
system_theme_settings_submit in modules/system/system.admin.inc
Process system_theme_settings form submissions.

... See full list

File

includes/file.inc, line 2217
API for handling file uploads and server file management.

Code

function file_get_mimetype($uri, $mapping = NULL) {
  if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
    return $wrapper
      ->getMimeType($uri, $mapping);
  }
  else {

    // getMimeType() is not implementation specific, so we can directly
    // call it without an instance.
    return DrupalLocalStreamWrapper::getMimeType($uri, $mapping);
  }
}

Comments

ph0enix’s picture

In Drupal 8 use :

\Drupal::service('file.mime_type.guesser')->guess($uri)