File.php 4.7 KB

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