public function ExtensionMimeTypeGuesser::guess in Zircon Profile 8.0
Same name in this branch
- 8.0 core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guess()
- 8.0 core/lib/Drupal/Core/ProxyClass/File/MimeType/ExtensionMimeTypeGuesser.php \Drupal\Core\ProxyClass\File\MimeType\ExtensionMimeTypeGuesser::guess()
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser::guess()
Guesses the mime type of the file with the given path.
Parameters
string $path The path to the file:
Return value
string The mime type or NULL, if none could be guessed
Throws
FileNotFoundException If the file does not exist
AccessDeniedException If the file could not be read
Overrides MimeTypeGuesserInterface::guess
File
- core/
lib/ Drupal/ Core/ File/ MimeType/ ExtensionMimeTypeGuesser.php, line 894 - Contains \Drupal\Core\File\MimeType\ExtensionMimeTypeGuesser.
Class
- ExtensionMimeTypeGuesser
- Makes possible to guess the MIME type of a file using its extension.
Namespace
Drupal\Core\File\MimeTypeCode
public function guess($path) {
if ($this->mapping === NULL) {
$mapping = $this->defaultMapping;
// Allow modules to alter the default mapping.
$this->moduleHandler
->alter('file_mimetype_mapping', $mapping);
$this->mapping = $mapping;
}
$extension = '';
$file_parts = explode('.', drupal_basename($path));
// Remove the first part: a full filename should not match an extension.
array_shift($file_parts);
// Iterate over the file parts, trying to find a match.
// For my.awesome.image.jpeg, we try:
// - jpeg
// - image.jpeg, and
// - awesome.image.jpeg
while ($additional_part = array_pop($file_parts)) {
$extension = strtolower($additional_part . ($extension ? '.' . $extension : ''));
if (isset($this->mapping['extensions'][$extension])) {
return $this->mapping['mimetypes'][$this->mapping['extensions'][$extension]];
}
}
return 'application/octet-stream';
}