public static function detectMimeType($fileName, $userFileName = '') {
        if (!file_exists($fileName)) {
            return '';
        }

        $mime = '';

        if (class_exists('finfo', false)) {
            $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;

            if (empty($mime)) {
                $mime = @finfo_open($const);
            }

            if (!empty($mime)) {
                $result = finfo_file($mime, $fileName);
            }
            unset($mime);
        }

        if (empty($result) && (function_exists('mime_content_type') && ini_get('mime_magic.magicfile'))) {
            $result = mime_content_type($fileName);
        }

        // dodatkowe sprawdzenie i korekta dla docx, xlsx, pptx
        if (empty($result) || $result == 'application/zip') {
            if (empty($userFileName)) {
                $userFileName = $fileName;
            }

            $pathParts = pathinfo($userFileName);
            if (isset($pathParts['extension'])) {
                switch ($pathParts['extension']) {
                    case '7z':
                        $result = 'application/x-7z-compressed';
                        break;
                    case 'xlsx':
                    case 'xltx':
                    case 'xlsm':
                    case 'xltm':
                    case 'xlam':
                    case 'xlsb':
                        $result = 'application/msexcel';
                        break;
                    case 'docx':
                    case 'dotx':
                    case 'docm':
                    case 'dotm':
                        $result = 'application/msword';
                        break;
                    case 'pptx':
                    case 'pptx':
                    case 'potx':
                    case 'ppsx':
                    case 'ppam':
                    case 'pptm':
                    case 'potm':
                    case 'ppsm':
                        $result = 'application/mspowerpoint';
                        break;
                    case 'vsd':
                    case 'vsdx':
                        $result = 'application/x-visio';
                        break;
                }
            }
        }

        if (empty($result)) {
            $result = 'application/octet-stream';
        }

        return $result;
    }