File.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\Kernel\Support;
  11. use finfo;
  12. /**
  13. * Class File.
  14. */
  15. class File
  16. {
  17. /**
  18. * MIME mapping.
  19. *
  20. * @var array
  21. */
  22. protected static $extensionMap = [
  23. 'audio/wav' => '.wav',
  24. 'audio/x-ms-wma' => '.wma',
  25. 'video/x-ms-wmv' => '.wmv',
  26. 'video/mp4' => '.mp4',
  27. 'audio/mpeg' => '.mp3',
  28. 'audio/amr' => '.amr',
  29. 'application/vnd.rn-realmedia' => '.rm',
  30. 'audio/mid' => '.mid',
  31. 'image/bmp' => '.bmp',
  32. 'image/gif' => '.gif',
  33. 'image/png' => '.png',
  34. 'image/tiff' => '.tiff',
  35. 'image/jpeg' => '.jpg',
  36. 'application/pdf' => '.pdf',
  37. // 列举更多的文件 mime, 企业号是支持的,公众平台这边之后万一也更新了呢
  38. 'application/msword' => '.doc',
  39. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx',
  40. 'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => '.dotx',
  41. 'application/vnd.ms-word.document.macroEnabled.12' => '.docm',
  42. 'application/vnd.ms-word.template.macroEnabled.12' => '.dotm',
  43. 'application/vnd.ms-excel' => '.xls',
  44. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx',
  45. 'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => '.xltx',
  46. 'application/vnd.ms-excel.sheet.macroEnabled.12' => '.xlsm',
  47. 'application/vnd.ms-excel.template.macroEnabled.12' => '.xltm',
  48. 'application/vnd.ms-excel.addin.macroEnabled.12' => '.xlam',
  49. 'application/vnd.ms-excel.sheet.binary.macroEnabled.12' => '.xlsb',
  50. 'application/vnd.ms-powerpoint' => '.ppt',
  51. 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx',
  52. 'application/vnd.openxmlformats-officedocument.presentationml.template' => '.potx',
  53. 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => '.ppsx',
  54. 'application/vnd.ms-powerpoint.addin.macroEnabled.12' => '.ppam',
  55. ];
  56. /**
  57. * File header signatures.
  58. *
  59. * @var array
  60. */
  61. protected static $signatures = [
  62. 'ffd8ff' => '.jpg',
  63. '424d' => '.bmp',
  64. '47494638' => '.gif',
  65. '2f55736572732f6f7665' => '.png',
  66. '89504e47' => '.png',
  67. '494433' => '.mp3',
  68. 'fffb' => '.mp3',
  69. 'fff3' => '.mp3',
  70. '3026b2758e66cf11' => '.wma',
  71. '52494646' => '.wav',
  72. '57415645' => '.wav',
  73. '41564920' => '.avi',
  74. '000001ba' => '.mpg',
  75. '000001b3' => '.mpg',
  76. '2321414d52' => '.amr',
  77. '25504446' => '.pdf',
  78. ];
  79. /**
  80. * Return steam extension.
  81. *
  82. * @param string $stream
  83. *
  84. * @return string|false
  85. */
  86. public static function getStreamExt($stream)
  87. {
  88. $ext = self::getExtBySignature($stream);
  89. try {
  90. if (empty($ext) && is_readable($stream)) {
  91. $stream = file_get_contents($stream);
  92. }
  93. } catch (\Exception $e) {
  94. }
  95. $fileInfo = new finfo(FILEINFO_MIME);
  96. $mime = strstr($fileInfo->buffer($stream), ';', true);
  97. return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : $ext;
  98. }
  99. /**
  100. * Get file extension by file header signature.
  101. *
  102. * @param string $stream
  103. *
  104. * @return string
  105. */
  106. public static function getExtBySignature($stream)
  107. {
  108. $prefix = strval(bin2hex(mb_strcut($stream, 0, 10)));
  109. foreach (self::$signatures as $signature => $extension) {
  110. if (0 === strpos($prefix, strval($signature))) {
  111. return $extension;
  112. }
  113. }
  114. return '';
  115. }
  116. }