F::mime PHP Method

mime() public static method

Returns the mime type of a file
public static mime ( string $file ) : mixed
$file string
return mixed
    public static function mime($file)
    {
        // stop for invalid files
        if (!file_exists($file)) {
            return null;
        }
        // Fileinfo is prefered if available
        if (function_exists('finfo_file')) {
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($finfo, $file);
            finfo_close($finfo);
            return $mime;
        }
        // for older versions with mime_content_type go for that.
        if (function_exists('mime_content_type') && ($mime = @mime_content_type($file) !== false)) {
            return $mime;
        }
        // shell check
        try {
            $mime = system::execute('file', [$file, '-z', '-b', '--mime'], 'output');
            $mime = trim(str::split($mime, ';')[0]);
            if (f::mimeToExtension($mime)) {
                return $mime;
            }
        } catch (Exception $e) {
            // no mime type detectable with shell
            $mime = null;
        }
        // Mime Sniffing
        $reader = new MimeReader($file);
        $mime = $reader->get_type();
        if (!empty($mime) && f::mimeToExtension($mime)) {
            return $mime;
        }
        // guess the matching mime type by extension
        return f::extensionToMime(f::extension($file));
    }