Back to MediaHelper class

Method getMimeType

public static mixed
getMimeType
(mixed $file, mixed $isImage = false)
Get the Mime type
Parameters
  • string $file The link to the file to be checked
  • bool $isImage True if the passed file is an image else false
Returns
  • mixed the mime type detected false on error
Since
  • 3.7.2
Class: MediaHelper
Project: Joomla

Method getMimeType - Source code

/**
 * Get the Mime type
 *
 * @param   string   $file     The link to the file to be checked
 * @param   boolean  $isImage  True if the passed file is an image else false
 *
 * @return  mixed    the mime type detected false on error
 *
 * @since   3.7.2
 */
public static function getMimeType($file, $isImage = false)
{
    // If we can't detect anything mime is false
    $mime = false;
    try {
        if ($isImage && \function_exists('exif_imagetype')) {
            $mime = image_type_to_mime_type(exif_imagetype($file));
        } elseif ($isImage && \function_exists('getimagesize')) {
            $imagesize = getimagesize($file);
            $mime = $imagesize['mime'] ?? false;
        } elseif (\function_exists('mime_content_type')) {
            // We have mime magic.
            $mime = mime_content_type($file);
        } elseif (\function_exists('finfo_open')) {
            // We have fileinfo
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($finfo, $file);
            finfo_close($finfo);
        }
    } catch (\Exception $e) {
        // If we have any kind of error here => false;
        return false;
    }
    // If we can't detect the mime try it again
    if ($mime === 'application/octet-stream' && $isImage === true) {
        $mime = static::getMimeType($file, false);
    }
    // We have a mime here
    return $mime;
}