diff --git a/application/Config/Mimes.php b/application/Config/Mimes.php index 25a9224f035c..bcb232181d92 100644 --- a/application/Config/Mimes.php +++ b/application/Config/Mimes.php @@ -302,13 +302,22 @@ public static function guessTypeFromExtension(string $extension) * Attempts to determine the best file extension for a given mime type. * * @param string $type + * @param string $proposed_extension - default extension (in case there is more than one with the same mime type) * * @return string|null The extension determined, or null if unable to match. */ - public static function guessExtensionFromType(string $type) + public static function guessExtensionFromType(string $type, ?string $proposed_extension = null) { + $type = trim(strtolower($type), '. '); + $proposed_extension = trim(strtolower($proposed_extension)); + + if(!is_null($proposed_extension) && array_key_exists($proposed_extension, self::$mimes) && in_array($type, self::$mimes[$proposed_extension])) + { + return $proposed_extension; + } + foreach (self::$mimes as $ext => $types) { if (is_string($types) && $types == $type) @@ -324,6 +333,7 @@ public static function guessExtensionFromType(string $type) return null; } + //-------------------------------------------------------------------- diff --git a/system/HTTP/Files/UploadedFile.php b/system/HTTP/Files/UploadedFile.php index 1e309616fd10..8d8d9087c611 100644 --- a/system/HTTP/Files/UploadedFile.php +++ b/system/HTTP/Files/UploadedFile.php @@ -342,12 +342,21 @@ public function getTempName(): string * * Is simply an alias for guessExtension for a safer method * than simply relying on the provided extension. + * Additionaly it will return clientExtension in case if there are + * other extensions withe the same mime type. */ - public function getExtension() + public function getExtension() : string { return $this->guessExtension(); } + public function guessExtension(): string + { + return \Config\Mimes::guessExtensionFromType($this->getMimeType(), $this->getClientExtension()); + } + + //-------------------------------------------------------------------- + /** * Returns the original file extension, based on the file name that * was uploaded. This is NOT a trusted source.