1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace ICanBoogie\HTTP;
13
14 15 16
17 class FileInfo
18 {
19 static public $types = [
20
21 '.doc' => 'application/msword',
22 '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
23 '.gif' => 'image/gif',
24 '.jpg' => 'image/jpeg',
25 '.jpeg' => 'image/jpeg',
26 '.js' => 'application/javascript',
27 '.json' => 'application/json',
28 '.mp3' => 'audio/mpeg',
29 '.odt' => 'application/vnd.oasis.opendocument.text',
30 '.pdf' => 'application/pdf',
31 '.php' => 'application/x-php',
32 '.png' => 'image/png',
33 '.psd' => 'application/psd',
34 '.rar' => 'application/rar',
35 '.txt' => 'text/plain',
36 '.zip' => 'application/zip',
37 '.xls' => 'application/vnd.ms-excel',
38 '.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
39
40 ];
41
42 static public $forced_types = [
43
44 '.js',
45 '.json',
46 '.php',
47 '.txt'
48
49 ];
50
51 static public $types_alias = [
52
53 'text/x-php' => 'application/x-php'
54
55 ];
56
57 58 59 60 61 62 63 64 65 66
67 static public function resolve_type($pathname, &$extension = null)
68 {
69 $extension = '.' . strtolower(pathinfo($pathname, PATHINFO_EXTENSION));
70
71 if (in_array($extension, self::$forced_types))
72 {
73 return self::$types[$extension];
74 }
75
76 if (file_exists($pathname) && extension_loaded('fileinfo'))
77 {
78 $fi = new \finfo(FILEINFO_MIME_TYPE);
79 $type = $fi->file($pathname);
80
81 if ($type)
82 {
83 return isset(self::$types_alias[$type]) ? self::$types_alias[$type] : $type;
84 }
85 }
86
87 if (isset(self::$types[$extension]))
88 {
89 return self::$types[$extension];
90 }
91
92 return 'application/octet-stream';
93 }
94 }
95