|
| 1 | +<?php |
| 2 | + |
| 3 | +if (!function_exists('mime_content_type')) { |
| 4 | + function mime_content_type($filename) |
| 5 | + { |
| 6 | + if (function_exists('finfo_open')) { |
| 7 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 8 | + $info = finfo_open(FILEINFO_MIME); |
| 9 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 10 | + $mime = finfo_file($info, $filename); |
| 11 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 12 | + finfo_close($info); |
| 13 | + return $mime; |
| 14 | + } |
| 15 | + $types = [ |
| 16 | + 'txt' => 'text/plain', |
| 17 | + 'htm' => 'text/html', |
| 18 | + 'html' => 'text/html', |
| 19 | + 'php' => 'text/html', |
| 20 | + 'css' => 'text/css', |
| 21 | + 'js' => 'application/javascript', |
| 22 | + 'json' => 'application/json', |
| 23 | + 'xml' => 'application/xml', |
| 24 | + 'swf' => 'application/x-shockwave-flash', |
| 25 | + 'flv' => 'video/x-flv', |
| 26 | + |
| 27 | + // images |
| 28 | + 'png' => 'image/png', |
| 29 | + 'jpe' => 'image/jpeg', |
| 30 | + 'jpeg' => 'image/jpeg', |
| 31 | + 'jpg' => 'image/jpeg', |
| 32 | + 'gif' => 'image/gif', |
| 33 | + 'bmp' => 'image/bmp', |
| 34 | + 'ico' => 'image/vnd.microsoft.icon', |
| 35 | + 'tiff' => 'image/tiff', |
| 36 | + 'tif' => 'image/tiff', |
| 37 | + 'svg' => 'image/svg+xml', |
| 38 | + 'svgz' => 'image/svg+xml', |
| 39 | + |
| 40 | + // archives |
| 41 | + 'zip' => 'application/zip', |
| 42 | + 'rar' => 'application/x-rar-compressed', |
| 43 | + 'exe' => 'application/x-msdownload', |
| 44 | + 'msi' => 'application/x-msdownload', |
| 45 | + 'cab' => 'application/vnd.ms-cab-compressed', |
| 46 | + |
| 47 | + // audio/video |
| 48 | + 'mp3' => 'audio/mpeg', |
| 49 | + 'qt' => 'video/quicktime', |
| 50 | + 'mov' => 'video/quicktime', |
| 51 | + |
| 52 | + // adobe |
| 53 | + 'pdf' => 'application/pdf', |
| 54 | + 'psd' => 'image/vnd.adobe.photoshop', |
| 55 | + 'ai' => 'application/postscript', |
| 56 | + 'eps' => 'application/postscript', |
| 57 | + 'ps' => 'application/postscript', |
| 58 | + |
| 59 | + // ms office |
| 60 | + 'doc' => 'application/msword', |
| 61 | + 'rtf' => 'application/rtf', |
| 62 | + 'xls' => 'application/vnd.ms-excel', |
| 63 | + 'ppt' => 'application/vnd.ms-powerpoint', |
| 64 | + |
| 65 | + // open office |
| 66 | + 'odt' => 'application/vnd.oasis.opendocument.text', |
| 67 | + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
| 68 | + ]; |
| 69 | + $arr = explode('.', $filename); |
| 70 | + $ext = strtolower(array_pop($arr)); |
| 71 | + if (array_key_exists($ext, $types)) { |
| 72 | + return $types[$ext]; |
| 73 | + } |
| 74 | + return 'application/octet-stream'; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function mime(string $file) |
| 79 | +{ |
| 80 | + if (file_exists($file)) { |
| 81 | + return mime_content_type($file); |
| 82 | + } |
| 83 | + if (function_exists('finfo_open')) { |
| 84 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 85 | + $info = finfo_open(FILEINFO_MIME); |
| 86 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 87 | + $mime = finfo_buffer($info, $file); |
| 88 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 89 | + finfo_close($info); |
| 90 | + return $mime; |
| 91 | + } |
| 92 | + return 'application/octet-stream'; |
| 93 | +} |
0 commit comments