mimeTypes = array_merge( [ 'doc' => 'application/msword', 'dot' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'docm' => 'application/vnd.ms-word.document.macroEnabled.12', 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12', 'xls' => 'application/vnd.ms-excel', 'xlt' => 'application/vnd.ms-excel', 'xla' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12', 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'ppt' => 'application/vnd.ms-powerpoint', 'pot' => 'application/vnd.ms-powerpoint', 'pps' => 'application/vnd.ms-powerpoint', 'ppa' => 'application/vnd.ms-powerpoint', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'mdb' => 'application/vnd.ms-access', 'aac' => 'audio/aac', 'abw' => 'application/x-abiword', 'arc' => 'application/octet-stream', 'avi' => 'video/x-msvideo', 'azw' => 'application/vnd.amazon.ebook', 'bin' => 'application/octet-stream', 'bz' => 'application/x-bzip', 'bz2' => 'application/x-bzip2', 'csh' => 'application/x-csh', 'css' => 'text/css', 'csv' => 'text/csv', 'eot' => 'application/vnd.ms-fontobject', 'epub' => 'application/epub+zip', 'gif' => 'image/gif', 'htm' => 'text/html', 'html' => 'text/html', 'twig' => 'text/html', 'ico' => 'image/x-icon', 'ics' => 'text/calendar', 'jar' => 'application/java-archive', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpg', 'js' => 'application/javascript', 'json' => 'application/json', 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mpeg' => 'video/mpeg', 'mpkg' => 'application/vnd.apple.installer+xml', 'odp' => 'application/vnd.oasis.opendocument.presentation', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 'odt' => 'application/vnd.oasis.opendocument.text', 'oga' => 'audio/ogg', 'ogv' => 'video/ogg', 'ogx' => 'application/ogg', 'otf' => 'font/otf', 'png' => 'image/png', 'pdf' => 'application/pdf', 'rar' => 'application/x-rar-compressed', 'rtf' => 'application/rtf', 'sh' => 'application/x-sh', 'svg' => 'image/svg+xml', 'swf' => 'application/x-shockwave-flash', 'tar' => 'application/x-tar', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'ts' => 'application/typescript', 'txt' => 'text/plain', 'ttf' => 'font/ttf', 'vsd' => 'application/vnd.visio', 'wav' => 'audio/x-wav', 'weba' => 'audio/webm', 'webm' => 'video/webm', 'webp' => 'image/webp', 'woff' => 'font/woff', 'woff2' => 'font/woff2', 'xhtml' => 'application/xhtml+xml', 'xml' => 'application/xml', 'xul' => 'application/vnd.mozilla.xul+xml', 'zip' => 'application/zip', '3gp' => 'video/3gpp', '3g2' => 'video/3gpp2', '7z' => 'application/x-7z-compressed' ], $extraMimes ); } /** * @param string $filename * @return string */ public function detectFromExtension(string $filename): string { return $this->mimeTypes[$this->getExtension($filename)] ?? 'text/plain'; } /** * @param string $filename * @return bool */ public function isText(string $filename): bool { $mime = $this->detectFromExtension($filename); if (fnmatch('text/*', $mime)) { return true; } $text = [ "application/javascript" => true, "application/rtf" => true, "application/json" => true, "application/x-sh" => true, "application/xml" => true ]; return isset($text[$mime]); } /** * @param $filename * @return string */ private function getExtension($filename): string { $file = basename($filename); $dotPlace = strrpos($file, '.'); if ($dotPlace === false) { return $file; } return substr($file, $dotPlace + 1); } }