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