河汉清且浅,牵牛敦而纯
随笔 - 77,  文章 - 0,  评论 - 1,  阅读 - 67049

 

 

 

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
通过文件头来判断文件类型
 
https://www.garykessler.net/library/file_sigs.html
 
https://en.wikipedia.org/wiki/List_of_file_signatures
 
https://gist.github.com/Qti3e/6341245314bf3513abb080677cd1c93b
 
https://gist.github.com/k1ic/ad805d5e3ee9931bea2c
 
https://gist.github.com/ldong/f334b9dcd421c99e094d
 
https://gist.github.com/soasme/6085733
 
 
 
php文件格式(mime类型)对照表
https://blog.csdn.net/q343509740/article/details/79616603
 
1.  mime_content_type返回指定文件的MIME类型,用法:
 
echo mime_content_type ( 'php.gif' ) . "\n" ;
echo mime_content_type ( 'test.php' );
 
2.  Fileinfo 获取文件MIME类型(finfo_open)
 
PHP官方推荐mime_content_type()的替代函数是Fileinfo函数。用法:
 
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
 
function getTypeList(){
    return array(
        "jpg"  =>    "FFD8FFE1",
        "png"  =>    "89504E47",
        "gif"  =>    "47494638",
        "tif"  =>    "49492A00",
        "bmp"  =>    "424D",
        "dwg"  =>    "41433130",
        "psd"  =>    "38425053",
        "rtf"  =>    "7B5C727466",
        "xml"  =>    "3C3F786D6C",
        "html  =>    "68746D6C3E",
        "eml"  =>    "44656C69766572792D646174",
        "dbx"  =>    "CFAD12FEC5FD746F",
        "pst"  =>    "2142444E",
        "xls"  =>    "D0CF11E0",
        "doc"  =>    "D0CF11E0",
        "mdb"  =>    "5374616E64617264204A",
        "wpd"  =>    "FF575043",
        "eps"  =>    "252150532D41646F6265",
        "ps"   =>    "252150532D41646F6265",
        "pdf"  =>    "255044462D312E",
        "pwl"  =>    "E3828596",
        "zip"  =>    "504B0304",
        "rar"  =>    "52617221",
        "wav"  =>    "57415645",
        "avi"  =>    "41564920",
        "ram"  =>    "2E7261FD",
        "rm"   =>    "2E524D46",
        "mpg"  =>    "000001BA",
        "mpg"  =>    "000001B3",
        "mov"  =>    "6D6F6F76",
        "asf"  =>    "3026B2758E66CF11",
        "mid"  =>    "4D546864",
        )
}
 
 
/**
 * 获取文件类型(通过读取文件前两个字节判断文件类型)
 * @param string $path 文件绝对路径
 * @return string 文件扩展名
 */
function get_file_type( $path = '' ) {
    $res = '';
    if ( file_exists($path) && is_readable($path) ) {
        $fh = fopen($path, 'rb');
        $bin = fread($fh, 2); //不一定只读前两个字节, 各个不同文件类型,头信息不一样。
        fclose($fh);
 
        $str_info  = unpack('C2chars', $bin); //"C2chars"中的“C”表示将给定二进制字符串解包为无符号字节型
        $type_code = intval($str_info['chars1'] . $str_info['chars2']);
        switch ( $type_code ) {
            case 3533:
                $res = 'amr';
                break;
            case 6677:
                $res = 'bmp';
                break;
            case 7790:
                $res = 'exe';
                break;
                break;
            case 7173:
                $res = 'gif';
                break;
            case 255216:
                $res = 'jpg';
                break;
            case 7368:
                $res = 'mp3';
                break;
            case 6063:
                $res = 'php';
                break;
            case 13780:
                $res = 'png';
                break;
            case 8297:
                $res = 'rar';
                break;
            case 4950:
                $res = 'txt';
                break;
            case 8075:
                $res = 'zip';
                break;
            default:
                $res = 'unknown' . $type_code;
        }
    } else {
        $res = !file_exists($path) ? 'file not exists' : ( !is_readable($path) ? 'not a readable file' : '' );
    }
    return $res;
}

  

posted on   伊索  阅读(483)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

点击右上角即可分享
微信分享提示