File.php 3.9 KB

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