File.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Reader\Utility;
  3. class File
  4. {
  5. // https://stackoverflow.com/questions/16511021/convert-mime-type-to-file-extension-php
  6. public static function mime2ext(string $mime): string
  7. {
  8. $mime_map = [
  9. 'video/3gpp2' => '3g2',
  10. 'video/3gp' => '3gp',
  11. 'video/3gpp' => '3gp',
  12. 'application/x-compressed' => '7zip',
  13. 'audio/x-acc' => 'aac',
  14. 'audio/ac3' => 'ac3',
  15. 'application/postscript' => 'ai',
  16. 'audio/x-aiff' => 'aif',
  17. 'audio/aiff' => 'aif',
  18. 'audio/x-au' => 'au',
  19. 'video/x-msvideo' => 'avi',
  20. 'video/msvideo' => 'avi',
  21. 'video/avi' => 'avi',
  22. 'application/x-troff-msvideo' => 'avi',
  23. 'application/macbinary' => 'bin',
  24. 'application/mac-binary' => 'bin',
  25. 'application/x-binary' => 'bin',
  26. 'application/x-macbinary' => 'bin',
  27. 'image/bmp' => 'bmp',
  28. 'image/x-bmp' => 'bmp',
  29. 'image/x-bitmap' => 'bmp',
  30. 'image/x-xbitmap' => 'bmp',
  31. 'image/x-win-bitmap' => 'bmp',
  32. 'image/x-windows-bmp' => 'bmp',
  33. 'image/ms-bmp' => 'bmp',
  34. 'image/x-ms-bmp' => 'bmp',
  35. 'application/bmp' => 'bmp',
  36. 'application/x-bmp' => 'bmp',
  37. 'application/x-win-bitmap' => 'bmp',
  38. 'application/cdr' => 'cdr',
  39. 'application/coreldraw' => 'cdr',
  40. 'application/x-cdr' => 'cdr',
  41. 'application/x-coreldraw' => 'cdr',
  42. 'image/cdr' => 'cdr',
  43. 'image/x-cdr' => 'cdr',
  44. 'zz-application/zz-winassoc-cdr' => 'cdr',
  45. 'application/mac-compactpro' => 'cpt',
  46. 'application/pkix-crl' => 'crl',
  47. 'application/pkcs-crl' => 'crl',
  48. 'application/x-x509-ca-cert' => 'crt',
  49. 'application/pkix-cert' => 'crt',
  50. 'text/css' => 'css',
  51. 'text/x-comma-separated-values' => 'csv',
  52. 'text/comma-separated-values' => 'csv',
  53. 'application/vnd.msexcel' => 'csv',
  54. 'application/x-director' => 'dcr',
  55. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
  56. 'application/x-dvi' => 'dvi',
  57. 'message/rfc822' => 'eml',
  58. 'application/x-msdownload' => 'exe',
  59. 'video/x-f4v' => 'f4v',
  60. 'audio/x-flac' => 'flac',
  61. 'video/x-flv' => 'flv',
  62. 'image/gif' => 'gif',
  63. 'application/gpg-keys' => 'gpg',
  64. 'application/x-gtar' => 'gtar',
  65. 'application/x-gzip' => 'gzip',
  66. 'application/mac-binhex40' => 'hqx',
  67. 'application/mac-binhex' => 'hqx',
  68. 'application/x-binhex40' => 'hqx',
  69. 'application/x-mac-binhex40' => 'hqx',
  70. 'text/html' => 'html',
  71. 'image/x-icon' => 'ico',
  72. 'image/x-ico' => 'ico',
  73. 'image/vnd.microsoft.icon' => 'ico',
  74. 'text/calendar' => 'ics',
  75. 'application/java-archive' => 'jar',
  76. 'application/x-java-application' => 'jar',
  77. 'application/x-jar' => 'jar',
  78. 'image/jp2' => 'jp2',
  79. 'video/mj2' => 'jp2',
  80. 'image/jpx' => 'jp2',
  81. 'image/jpm' => 'jp2',
  82. 'image/jpeg' => 'jpeg',
  83. 'image/pjpeg' => 'jpeg',
  84. 'application/x-javascript' => 'js',
  85. 'application/json' => 'json',
  86. 'text/json' => 'json',
  87. 'application/vnd.google-earth.kml+xml' => 'kml',
  88. 'application/vnd.google-earth.kmz' => 'kmz',
  89. 'text/x-log' => 'log',
  90. 'audio/x-m4a' => 'm4a',
  91. 'audio/mp4' => 'm4a',
  92. 'application/vnd.mpegurl' => 'm4u',
  93. 'audio/midi' => 'mid',
  94. 'application/vnd.mif' => 'mif',
  95. 'video/quicktime' => 'mov',
  96. 'video/x-sgi-movie' => 'movie',
  97. 'audio/mpeg' => 'mp3',
  98. 'audio/mpg' => 'mp3',
  99. 'audio/mpeg3' => 'mp3',
  100. 'audio/mp3' => 'mp3',
  101. 'video/mp4' => 'mp4',
  102. 'video/mpeg' => 'mpeg',
  103. 'application/oda' => 'oda',
  104. 'audio/ogg' => 'ogg',
  105. 'video/ogg' => 'ogg',
  106. 'application/ogg' => 'ogg',
  107. 'font/otf' => 'otf',
  108. 'application/x-pkcs10' => 'p10',
  109. 'application/pkcs10' => 'p10',
  110. 'application/x-pkcs12' => 'p12',
  111. 'application/x-pkcs7-signature' => 'p7a',
  112. 'application/pkcs7-mime' => 'p7c',
  113. 'application/x-pkcs7-mime' => 'p7c',
  114. 'application/x-pkcs7-certreqresp' => 'p7r',
  115. 'application/pkcs7-signature' => 'p7s',
  116. 'application/pdf' => 'pdf',
  117. 'application/octet-stream' => 'pdf',
  118. 'application/x-x509-user-cert' => 'pem',
  119. 'application/x-pem-file' => 'pem',
  120. 'application/pgp' => 'pgp',
  121. 'application/x-httpd-php' => 'php',
  122. 'application/php' => 'php',
  123. 'application/x-php' => 'php',
  124. 'text/php' => 'php',
  125. 'text/x-php' => 'php',
  126. 'application/x-httpd-php-source' => 'php',
  127. 'image/png' => 'png',
  128. 'image/x-png' => 'png',
  129. 'application/powerpoint' => 'ppt',
  130. 'application/vnd.ms-powerpoint' => 'ppt',
  131. 'application/vnd.ms-office' => 'ppt',
  132. 'application/msword' => 'doc',
  133. 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
  134. 'application/x-photoshop' => 'psd',
  135. 'image/vnd.adobe.photoshop' => 'psd',
  136. 'audio/x-realaudio' => 'ra',
  137. 'audio/x-pn-realaudio' => 'ram',
  138. 'application/x-rar' => 'rar',
  139. 'application/rar' => 'rar',
  140. 'application/x-rar-compressed' => 'rar',
  141. 'audio/x-pn-realaudio-plugin' => 'rpm',
  142. 'application/x-pkcs7' => 'rsa',
  143. 'text/rtf' => 'rtf',
  144. 'text/richtext' => 'rtx',
  145. 'video/vnd.rn-realvideo' => 'rv',
  146. 'application/x-stuffit' => 'sit',
  147. 'application/smil' => 'smil',
  148. 'text/srt' => 'srt',
  149. 'image/svg+xml' => 'svg',
  150. 'application/x-shockwave-flash' => 'swf',
  151. 'application/x-tar' => 'tar',
  152. 'application/x-gzip-compressed' => 'tgz',
  153. 'image/tiff' => 'tiff',
  154. 'font/ttf' => 'ttf',
  155. 'text/plain' => 'txt',
  156. 'text/x-vcard' => 'vcf',
  157. 'application/videolan' => 'vlc',
  158. 'text/vtt' => 'vtt',
  159. 'audio/x-wav' => 'wav',
  160. 'audio/wave' => 'wav',
  161. 'audio/wav' => 'wav',
  162. 'application/wbxml' => 'wbxml',
  163. 'video/webm' => 'webm',
  164. 'image/webp' => 'webp',
  165. 'audio/x-ms-wma' => 'wma',
  166. 'application/wmlc' => 'wmlc',
  167. 'video/x-ms-wmv' => 'wmv',
  168. 'video/x-ms-asf' => 'wmv',
  169. 'font/woff' => 'woff',
  170. 'font/woff2' => 'woff2',
  171. 'application/xhtml+xml' => 'xhtml',
  172. 'application/excel' => 'xl',
  173. 'application/msexcel' => 'xls',
  174. 'application/x-msexcel' => 'xls',
  175. 'application/x-ms-excel' => 'xls',
  176. 'application/x-excel' => 'xls',
  177. 'application/x-dos_ms_excel' => 'xls',
  178. 'application/xls' => 'xls',
  179. 'application/x-xls' => 'xls',
  180. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
  181. 'application/vnd.ms-excel' => 'xlsx',
  182. 'application/xml' => 'xml',
  183. 'text/xml' => 'xml',
  184. 'text/xsl' => 'xsl',
  185. 'application/xspf+xml' => 'xspf',
  186. 'application/x-compress' => 'z',
  187. 'application/x-zip' => 'zip',
  188. 'application/zip' => 'zip',
  189. 'application/x-zip-compressed' => 'zip',
  190. 'application/s-compressed' => 'zip',
  191. 'multipart/x-zip' => 'zip',
  192. 'text/x-scriptzsh' => 'zsh',
  193. ];
  194. return $mime_map[$mime] ?? '';
  195. }
  196. }