Mimes.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. namespace Config;
  3. /**
  4. * Mimes
  5. *
  6. * This file contains an array of mime types. It is used by the
  7. * Upload class to help identify allowed file types.
  8. *
  9. * When more than one variation for an extension exist (like jpg, jpeg, etc)
  10. * the most common one should be first in the array to aid the guess*
  11. * methods. The same applies when more than one mime-type exists for a
  12. * single extension.
  13. *
  14. * When working with mime types, please make sure you have the ´fileinfo´
  15. * extension enabled to reliably detect the media types.
  16. *
  17. * @immutable
  18. */
  19. class Mimes
  20. {
  21. /**
  22. * Map of extensions to mime types.
  23. *
  24. * @var array<string, list<string>|string>
  25. */
  26. public static array $mimes = [
  27. 'hqx' => [
  28. 'application/mac-binhex40',
  29. 'application/mac-binhex',
  30. 'application/x-binhex40',
  31. 'application/x-mac-binhex40',
  32. ],
  33. 'cpt' => 'application/mac-compactpro',
  34. 'csv' => [
  35. 'text/csv',
  36. 'text/x-comma-separated-values',
  37. 'text/comma-separated-values',
  38. 'application/vnd.ms-excel',
  39. 'application/x-csv',
  40. 'text/x-csv',
  41. 'application/csv',
  42. 'application/excel',
  43. 'application/vnd.msexcel',
  44. 'text/plain',
  45. ],
  46. 'bin' => [
  47. 'application/macbinary',
  48. 'application/mac-binary',
  49. 'application/octet-stream',
  50. 'application/x-binary',
  51. 'application/x-macbinary',
  52. ],
  53. 'dms' => 'application/octet-stream',
  54. 'lha' => 'application/octet-stream',
  55. 'lzh' => 'application/octet-stream',
  56. 'exe' => [
  57. 'application/octet-stream',
  58. 'application/vnd.microsoft.portable-executable',
  59. 'application/x-dosexec',
  60. 'application/x-msdownload',
  61. ],
  62. 'class' => 'application/octet-stream',
  63. 'psd' => [
  64. 'application/x-photoshop',
  65. 'image/vnd.adobe.photoshop',
  66. ],
  67. 'so' => 'application/octet-stream',
  68. 'sea' => 'application/octet-stream',
  69. 'dll' => 'application/octet-stream',
  70. 'oda' => 'application/oda',
  71. 'pdf' => [
  72. 'application/pdf',
  73. 'application/force-download',
  74. 'application/x-download',
  75. ],
  76. 'ai' => [
  77. 'application/pdf',
  78. 'application/postscript',
  79. ],
  80. 'eps' => 'application/postscript',
  81. 'ps' => 'application/postscript',
  82. 'smi' => 'application/smil',
  83. 'smil' => 'application/smil',
  84. 'mif' => 'application/vnd.mif',
  85. 'xls' => [
  86. 'application/vnd.ms-excel',
  87. 'application/msexcel',
  88. 'application/x-msexcel',
  89. 'application/x-ms-excel',
  90. 'application/x-excel',
  91. 'application/x-dos_ms_excel',
  92. 'application/xls',
  93. 'application/x-xls',
  94. 'application/excel',
  95. 'application/download',
  96. 'application/vnd.ms-office',
  97. 'application/msword',
  98. ],
  99. 'ppt' => [
  100. 'application/vnd.ms-powerpoint',
  101. 'application/powerpoint',
  102. 'application/vnd.ms-office',
  103. 'application/msword',
  104. ],
  105. 'pptx' => [
  106. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  107. ],
  108. 'wbxml' => 'application/wbxml',
  109. 'wmlc' => 'application/wmlc',
  110. 'dcr' => 'application/x-director',
  111. 'dir' => 'application/x-director',
  112. 'dxr' => 'application/x-director',
  113. 'dvi' => 'application/x-dvi',
  114. 'gtar' => 'application/x-gtar',
  115. 'gz' => 'application/x-gzip',
  116. 'gzip' => 'application/x-gzip',
  117. 'php' => [
  118. 'application/x-php',
  119. 'application/x-httpd-php',
  120. 'application/php',
  121. 'text/php',
  122. 'text/x-php',
  123. 'application/x-httpd-php-source',
  124. ],
  125. 'php4' => 'application/x-httpd-php',
  126. 'php3' => 'application/x-httpd-php',
  127. 'phtml' => 'application/x-httpd-php',
  128. 'phps' => 'application/x-httpd-php-source',
  129. 'js' => [
  130. 'application/x-javascript',
  131. 'text/plain',
  132. ],
  133. 'swf' => 'application/x-shockwave-flash',
  134. 'sit' => 'application/x-stuffit',
  135. 'tar' => 'application/x-tar',
  136. 'tgz' => [
  137. 'application/x-tar',
  138. 'application/x-gzip-compressed',
  139. ],
  140. 'z' => 'application/x-compress',
  141. 'xhtml' => 'application/xhtml+xml',
  142. 'xht' => 'application/xhtml+xml',
  143. 'zip' => [
  144. 'application/x-zip',
  145. 'application/zip',
  146. 'application/x-zip-compressed',
  147. 'application/s-compressed',
  148. 'multipart/x-zip',
  149. ],
  150. 'rar' => [
  151. 'application/vnd.rar',
  152. 'application/x-rar',
  153. 'application/rar',
  154. 'application/x-rar-compressed',
  155. ],
  156. 'mid' => 'audio/midi',
  157. 'midi' => 'audio/midi',
  158. 'mpga' => 'audio/mpeg',
  159. 'mp2' => 'audio/mpeg',
  160. 'mp3' => [
  161. 'audio/mpeg',
  162. 'audio/mpg',
  163. 'audio/mpeg3',
  164. 'audio/mp3',
  165. ],
  166. 'aif' => [
  167. 'audio/x-aiff',
  168. 'audio/aiff',
  169. ],
  170. 'aiff' => [
  171. 'audio/x-aiff',
  172. 'audio/aiff',
  173. ],
  174. 'aifc' => 'audio/x-aiff',
  175. 'ram' => 'audio/x-pn-realaudio',
  176. 'rm' => 'audio/x-pn-realaudio',
  177. 'rpm' => 'audio/x-pn-realaudio-plugin',
  178. 'ra' => 'audio/x-realaudio',
  179. 'rv' => 'video/vnd.rn-realvideo',
  180. 'wav' => [
  181. 'audio/x-wav',
  182. 'audio/wave',
  183. 'audio/wav',
  184. ],
  185. 'bmp' => [
  186. 'image/bmp',
  187. 'image/x-bmp',
  188. 'image/x-bitmap',
  189. 'image/x-xbitmap',
  190. 'image/x-win-bitmap',
  191. 'image/x-windows-bmp',
  192. 'image/ms-bmp',
  193. 'image/x-ms-bmp',
  194. 'application/bmp',
  195. 'application/x-bmp',
  196. 'application/x-win-bitmap',
  197. ],
  198. 'gif' => 'image/gif',
  199. 'jpg' => [
  200. 'image/jpeg',
  201. 'image/pjpeg',
  202. ],
  203. 'jpeg' => [
  204. 'image/jpeg',
  205. 'image/pjpeg',
  206. ],
  207. 'jpe' => [
  208. 'image/jpeg',
  209. 'image/pjpeg',
  210. ],
  211. 'jp2' => [
  212. 'image/jp2',
  213. 'video/mj2',
  214. 'image/jpx',
  215. 'image/jpm',
  216. ],
  217. 'j2k' => [
  218. 'image/jp2',
  219. 'video/mj2',
  220. 'image/jpx',
  221. 'image/jpm',
  222. ],
  223. 'jpf' => [
  224. 'image/jp2',
  225. 'video/mj2',
  226. 'image/jpx',
  227. 'image/jpm',
  228. ],
  229. 'jpg2' => [
  230. 'image/jp2',
  231. 'video/mj2',
  232. 'image/jpx',
  233. 'image/jpm',
  234. ],
  235. 'jpx' => [
  236. 'image/jp2',
  237. 'video/mj2',
  238. 'image/jpx',
  239. 'image/jpm',
  240. ],
  241. 'jpm' => [
  242. 'image/jp2',
  243. 'video/mj2',
  244. 'image/jpx',
  245. 'image/jpm',
  246. ],
  247. 'mj2' => [
  248. 'image/jp2',
  249. 'video/mj2',
  250. 'image/jpx',
  251. 'image/jpm',
  252. ],
  253. 'mjp2' => [
  254. 'image/jp2',
  255. 'video/mj2',
  256. 'image/jpx',
  257. 'image/jpm',
  258. ],
  259. 'png' => [
  260. 'image/png',
  261. 'image/x-png',
  262. ],
  263. 'webp' => 'image/webp',
  264. 'tif' => 'image/tiff',
  265. 'tiff' => 'image/tiff',
  266. 'css' => [
  267. 'text/css',
  268. 'text/plain',
  269. ],
  270. 'html' => [
  271. 'text/html',
  272. 'text/plain',
  273. ],
  274. 'htm' => [
  275. 'text/html',
  276. 'text/plain',
  277. ],
  278. 'shtml' => [
  279. 'text/html',
  280. 'text/plain',
  281. ],
  282. 'txt' => 'text/plain',
  283. 'text' => 'text/plain',
  284. 'log' => [
  285. 'text/plain',
  286. 'text/x-log',
  287. ],
  288. 'rtx' => 'text/richtext',
  289. 'rtf' => 'text/rtf',
  290. 'xml' => [
  291. 'application/xml',
  292. 'text/xml',
  293. 'text/plain',
  294. ],
  295. 'xsl' => [
  296. 'application/xml',
  297. 'text/xsl',
  298. 'text/xml',
  299. ],
  300. 'mpeg' => 'video/mpeg',
  301. 'mpg' => 'video/mpeg',
  302. 'mpe' => 'video/mpeg',
  303. 'qt' => 'video/quicktime',
  304. 'mov' => 'video/quicktime',
  305. 'avi' => [
  306. 'video/x-msvideo',
  307. 'video/msvideo',
  308. 'video/avi',
  309. 'application/x-troff-msvideo',
  310. ],
  311. 'movie' => 'video/x-sgi-movie',
  312. 'doc' => [
  313. 'application/msword',
  314. 'application/vnd.ms-office',
  315. ],
  316. 'docx' => [
  317. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  318. 'application/zip',
  319. 'application/msword',
  320. 'application/x-zip',
  321. ],
  322. 'dot' => [
  323. 'application/msword',
  324. 'application/vnd.ms-office',
  325. ],
  326. 'dotx' => [
  327. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  328. 'application/zip',
  329. 'application/msword',
  330. ],
  331. 'xlsx' => [
  332. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  333. 'application/zip',
  334. 'application/vnd.ms-excel',
  335. 'application/msword',
  336. 'application/x-zip',
  337. ],
  338. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  339. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  340. 'word' => [
  341. 'application/msword',
  342. 'application/octet-stream',
  343. ],
  344. 'xl' => 'application/excel',
  345. 'eml' => 'message/rfc822',
  346. 'json' => [
  347. 'application/json',
  348. 'text/json',
  349. ],
  350. 'pem' => [
  351. 'application/x-x509-user-cert',
  352. 'application/x-pem-file',
  353. 'application/octet-stream',
  354. ],
  355. 'p10' => [
  356. 'application/x-pkcs10',
  357. 'application/pkcs10',
  358. ],
  359. 'p12' => 'application/x-pkcs12',
  360. 'p7a' => 'application/x-pkcs7-signature',
  361. 'p7c' => [
  362. 'application/pkcs7-mime',
  363. 'application/x-pkcs7-mime',
  364. ],
  365. 'p7m' => [
  366. 'application/pkcs7-mime',
  367. 'application/x-pkcs7-mime',
  368. ],
  369. 'p7r' => 'application/x-pkcs7-certreqresp',
  370. 'p7s' => 'application/pkcs7-signature',
  371. 'crt' => [
  372. 'application/x-x509-ca-cert',
  373. 'application/x-x509-user-cert',
  374. 'application/pkix-cert',
  375. ],
  376. 'crl' => [
  377. 'application/pkix-crl',
  378. 'application/pkcs-crl',
  379. ],
  380. 'der' => 'application/x-x509-ca-cert',
  381. 'kdb' => 'application/octet-stream',
  382. 'pgp' => 'application/pgp',
  383. 'gpg' => 'application/gpg-keys',
  384. 'sst' => 'application/octet-stream',
  385. 'csr' => 'application/octet-stream',
  386. 'rsa' => 'application/x-pkcs7',
  387. 'cer' => [
  388. 'application/pkix-cert',
  389. 'application/x-x509-ca-cert',
  390. ],
  391. '3g2' => 'video/3gpp2',
  392. '3gp' => [
  393. 'video/3gp',
  394. 'video/3gpp',
  395. ],
  396. 'mp4' => 'video/mp4',
  397. 'm4a' => 'audio/x-m4a',
  398. 'f4v' => [
  399. 'video/mp4',
  400. 'video/x-f4v',
  401. ],
  402. 'flv' => 'video/x-flv',
  403. 'webm' => 'video/webm',
  404. 'aac' => 'audio/x-acc',
  405. 'm4u' => 'application/vnd.mpegurl',
  406. 'm3u' => 'text/plain',
  407. 'xspf' => 'application/xspf+xml',
  408. 'vlc' => 'application/videolan',
  409. 'wmv' => [
  410. 'video/x-ms-wmv',
  411. 'video/x-ms-asf',
  412. ],
  413. 'au' => 'audio/x-au',
  414. 'ac3' => 'audio/ac3',
  415. 'flac' => 'audio/x-flac',
  416. 'ogg' => [
  417. 'audio/ogg',
  418. 'video/ogg',
  419. 'application/ogg',
  420. ],
  421. 'kmz' => [
  422. 'application/vnd.google-earth.kmz',
  423. 'application/zip',
  424. 'application/x-zip',
  425. ],
  426. 'kml' => [
  427. 'application/vnd.google-earth.kml+xml',
  428. 'application/xml',
  429. 'text/xml',
  430. ],
  431. 'ics' => 'text/calendar',
  432. 'ical' => 'text/calendar',
  433. 'zsh' => 'text/x-scriptzsh',
  434. '7zip' => [
  435. 'application/x-compressed',
  436. 'application/x-zip-compressed',
  437. 'application/zip',
  438. 'multipart/x-zip',
  439. ],
  440. 'cdr' => [
  441. 'application/cdr',
  442. 'application/coreldraw',
  443. 'application/x-cdr',
  444. 'application/x-coreldraw',
  445. 'image/cdr',
  446. 'image/x-cdr',
  447. 'zz-application/zz-winassoc-cdr',
  448. ],
  449. 'wma' => [
  450. 'audio/x-ms-wma',
  451. 'video/x-ms-asf',
  452. ],
  453. 'jar' => [
  454. 'application/java-archive',
  455. 'application/x-java-application',
  456. 'application/x-jar',
  457. 'application/x-compressed',
  458. ],
  459. 'svg' => [
  460. 'image/svg+xml',
  461. 'image/svg',
  462. 'application/xml',
  463. 'text/xml',
  464. ],
  465. 'vcf' => 'text/x-vcard',
  466. 'srt' => [
  467. 'text/srt',
  468. 'text/plain',
  469. ],
  470. 'vtt' => [
  471. 'text/vtt',
  472. 'text/plain',
  473. ],
  474. 'ico' => [
  475. 'image/x-icon',
  476. 'image/x-ico',
  477. 'image/vnd.microsoft.icon',
  478. ],
  479. 'stl' => [
  480. 'application/sla',
  481. 'application/vnd.ms-pki.stl',
  482. 'application/x-navistyle',
  483. ],
  484. ];
  485. /**
  486. * Attempts to determine the best mime type for the given file extension.
  487. *
  488. * @return string|null The mime type found, or none if unable to determine.
  489. */
  490. public static function guessTypeFromExtension(string $extension)
  491. {
  492. $extension = trim(strtolower($extension), '. ');
  493. if (! array_key_exists($extension, static::$mimes)) {
  494. return null;
  495. }
  496. return is_array(static::$mimes[$extension]) ? static::$mimes[$extension][0] : static::$mimes[$extension];
  497. }
  498. /**
  499. * Attempts to determine the best file extension for a given mime type.
  500. *
  501. * @param string|null $proposedExtension - default extension (in case there is more than one with the same mime type)
  502. *
  503. * @return string|null The extension determined, or null if unable to match.
  504. */
  505. public static function guessExtensionFromType(string $type, ?string $proposedExtension = null)
  506. {
  507. $type = trim(strtolower($type), '. ');
  508. $proposedExtension = trim(strtolower($proposedExtension ?? ''));
  509. if (
  510. $proposedExtension !== ''
  511. && array_key_exists($proposedExtension, static::$mimes)
  512. && in_array($type, (array) static::$mimes[$proposedExtension], true)
  513. ) {
  514. // The detected mime type matches with the proposed extension.
  515. return $proposedExtension;
  516. }
  517. // Reverse check the mime type list if no extension was proposed.
  518. // This search is order sensitive!
  519. foreach (static::$mimes as $ext => $types) {
  520. if (in_array($type, (array) $types, true)) {
  521. return $ext;
  522. }
  523. }
  524. return null;
  525. }
  526. }