首頁  >  文章  >  後端開發  >  php根據文件頭驗證文件類型的程式碼

php根據文件頭驗證文件類型的程式碼

WBOY
WBOY原創
2016-07-25 09:00:441614瀏覽
  1. /**
  2. * 檢證文件類型類別
  3. *
  4. * @author Silver
  5. * by bbs.it-home.org
  6. */
  7. class FileTypeValidation
  8. {
  9. // 檔案類型,不同的頭資訊
  10. private static $_fileFormats = Array(
  11. 'jp2' => '0000000C6A502020',
    '3gp' => '0000002066747970',
  12. '355' 'm4a' => '00000020667479704D3441',
  13. 'ico' => '00000100',
  14. 'spl' => '00000100',
  15. 'cura =1000 ' => '00000200',
  16. 'wb2' => '00000200',
  17. 'wk1' => '0000020006040600',
  18. 'wk3' => '0001140600, > '00001A0002100400',
  19. 'wk5' => '00001A0002100400',
  20. '123' => '00001A00051004', ' 010F0000',
  21. 'tr1' => '0110',
  22. 'rgb' => '01DA01010003',
  23. 'drw' => '07',
  24. 'dss' => '0264drw' => '07',
  25. 'dss' => '02647333' ,
  26. 'dat' => 'A90D000000000000',
  27. 'db3' => '03',
  28. 'qph' => '03000000',
  29. 'adx' => '8000044' 🎜>'db4' => '04',
  30. 'n' => 'FFFE0000',
  31. 'a' => 'FFFE0000',
  32. 'skf' => '07534B46',
  33. 'dtd' => '0764743264647464',
  34. 'db' => 'D0CF11E0A1B11AE1',
  35. 'pcx' => '0A050101',
  36. 'mp' => '0CEDED',d. ' => 'D0CF11E0A1B11AE1',
  37. 'nri' => '0E4E65726F49534F',
  38. 'wks' => 'FF00020004040554',pf > '4E49544630',
  39. 'nsf' => '4E45534D1A01',
  40. 'arc' => '41724301',
  41. 'pak' => '5041434B',
  42. ' 1A350100',
  43. 'mkv' => '1A45DFA393428288',
  44. 'ws' => '1D7D',
  45. 'gz' => '1F8B08',
  46. 'tar.z' 1FA0',
  47. 'ain' => '2112',
  48. 'lib' => '213C617263683E0A',
  49. 'msi' => 'D0CF11E0A1B11AE1',
  50. '4vmd ,
  51. 'dsp' => '23204D6963726F73',
  52. 'amr' => '2321414D52',
  53. 'hdr' => '49536328',
  54. 's13'hdr' => '49536328',
  55. 's03'hdr' => '49536328','eps' => 'C5D0D3C6',
  56. 'pdf' => '25504446',
  57. 'fdf' => '25504446',
  58. 'hqx' => '28546869732 '2A2A2A2020496E73',
  59. 'ivr' => '2E524543',
  60. 'rm' => '2E524D46',
  61. 'rmvb' => '2E524466' ' => '2E7261FD00',
  62. 'au' => '646E732E',
  63. 'cat' => '30',
  64. 'evt' => '300000004C664C65',
  65. ' > '3026B2758E66CF11',
  66. 'wma' => '3026B2758E66CF11',
  67. 'wmv' => '3026B2758E66CF11',
  68. 'w'🎜> 'w 377ABCAF271C',
  69. 'psd' => '38425053',
  70. 'sle' => '414376',
  71. 'asx' => '3C',
  72. 'xdr' => '3C' ,
  73. 'dci' => '3C21646F63747970',
  74. 'manifest' => '3C3F786D6C2076657273696F6E3D',
  75. 'xml7636 30223F3E',
  76. 'msc' => 'D0CF11E0A1B11AE1',
  77. 'fm' => '3C4D616B65724669',
  78. 'mif' => '56657273696F6E20',
  79. 'gid' => '4C4E0200',

    '41433130',

  80. 'syw' => '414D594F',
  81. 'abi' => '414F4C494E444558',
  82. 'aby' => '414F444558',
  83. 'aby' => '414F4444447 '414F4C2046656564',
  84. 'idx' => '5000000020000000',
  85. 'ind' => '414F4C494458',
  86. 'ind' => '414F4C494458',
  87. 'pfc' = '414F4C494458',
  88. 'pfc' = '414F4C494458',
  89. 'pfc' = '414305,4643')> > '414F4C564D313030',
  90. 'vcf' => '424547494E3A5643',
  91. 'bin' => '424C4932323351',
  92. 'bdi' = ' 424D',
  93. 'prc' => '424F4F4B4D4F4249',
  94. 'bz2' => '425A68',
  95. 'tar.bz2' => '425A68', 'tbz>' 425A68',
  96. 'tb2' => '425A68',
  97. 'rtd' => '43232B44A4434DA5',
  98. 'cbd' => '434246494C45','clb' => '434F4D2B',
  99. 'cpt' => '43505446494C45',
  100. 'cru' => '43525553482076',
  101. '4446 🎜>'ctf' => '436174616C6F6720',
  102. 'dms' => '444D5321',
  103. 'adf' => '5245564E554D3A2C', 'ifo' => '445644',
  104. 'cdr' => '52494646',
  105. 'vcd' => '454E545259564344',
  106. 'mdi' => '4550',' ' => '4C5646090D0AFF00',
  107. 'evtx' => '456C6646696C6500',
  108. 'qbb' => '458600000600',
  109. 'qbb' => '458600000600',
  110. 'c5',43450,430,430,4300,43'> > '464C56',
  111. 'aiff' => '464F524D00',
  112. 'eml' => '582D',
  113. 'gif' => '47494638',
  114. 'pat' => ' 47504154',
  115. 'gx2' => '475832',
  116. 'sh3' => '4848474231',
  117. 'tif' => '4D4D002B',
  118. 'tiff' ,
  119. 'mp3' => '494433',
  120. 'koz' => '49443303000000',
  121. 'crw' => '49491A0000004845',
  122. '49491A0000004845',
  123. 'lit' => '49544F4C49544C53',
  124. 'chi' => '49545346',
  125. 'chm' => '49545346',
  126. '罐子' => '5F27A889',
  127. 'jg' =>; '4A47040E000000',
  128. '克格勃' => '4B47425F61726368',
  129. 'shd'🎜>' lnk'=>; '4C00000001140200',
  130. 'obj'=>; '80',
  131. 'pdb'=>; 'ACED000573720012',
  132. 'mar' =>ACED00573720012',
  133. 'mar' =>>; >'dmp' =>; '504147454455',
  134. 'hdmp'=> '4D444D5093A7',
  135. 'mls' => '4D563243',
  136. 'mmf' =>; >'nvram'=>; '4D52564E',
  137. 'ppz' => '4D534346',
  138. 'snp' => '4D534346',
  139. 'tlb' => '4D53465402000 'dvf' =>; '4D535F564F494345',
  140. 'msv'=>; '4D535F564F494345',
  141. 'mid' => '4D546864',
  142. 'm
  143. 'dsn' => '4D56',
  144. 'com' =>; 'EB',
  145. 'dll' =>; '4D5A',
  146. 'drv' => '4D5A',
  147. 'exe' =>; '4D5A',
  148. 'pif' =>; '4D5A',
  149. 'qts' =>; '4D5A',
  150. 'qtx'=>; '4D5A',
  151. 'sys'=>; 'FFFFFFFF',
  152. 'acm' =>; '4D5A',
  153. '斧頭' => '4D5A900003000000',
  154. 'cpl' =>; 'DCDC',
  155. 'fon' =>; '4D5A',
  156. 'ocx' =>; '4D5A',
  157. 'olb' => '4D5A',
  158. 'scr' =>; '4D5A' ,
  159. 'vbx'=>; '4D5A',
  160. 'vxd'=>; '4D5A',
  161. '386' => '4D5A',
  162. 'api' => '4D5A900003000000' ,
  163. 'flt' => '76323030332E3130',
  164. 'zap' => '4D5A90000300000004000000FFFF',
  165. 'sln' =>'jnt'=>; '4E422A00' ,
  166. 'jtp' =>; '4E422A00',
  167. '鱈魚' => '4E616D653A20',
  168. 'dbf' =>; '4F504C4461746162','ogg'=> '4F67675300020000',
  169. 'ogv' => '4F67675300020000',
  170. 'ogx' => '4F676753002000,400,400'>
  171. 'pgm' =>; '50350A',
  172. 'pax'=> '504158',
  173. 'pgd'=>; '504750644D41494E',
  174. 'img' =AEB3C90
  175. 'zip' =>; '504B0304140000',
  176. 'docx'=>; '504B030414000600',
  177. 'pptx'=>; '504B030600',
  178. 'pptx'=>; '504B03041400600'> 00 ',
  179. 'kwd'=>; '504B0304',
  180. 'odt'=>; '504B0304',
  181. 'odp'=>; '504B0304',
  182. '奧特' => '504B0304',
  183. 'sxc'=>; '504B0304',
  184. 'sxd'=> '504B0304',
  185. 'sxi'=>; '504B0304',
  186. 'sxi'=>; '504B0304','sx '504B0304',
  187. 'wmz'=> '504B0304',
  188. 'xpi'=>; '504B0304',
  189. 'xps' => '504B0304',
  190. 'x
  191. 'x'pt' '5850434F4D0A5479',
  192. 'grp' =>; '504D4343',
  193. 'qemu'=>; '514649',
  194. 'abd' => '5157205655720565572> '5157205665722E20',
  195. 'reg'=> 'FFFE',
  196. 'sud' =>; '52454745444954',
  197. 'ani' =>; '52494646','ds4' => '52494646',
  198. '4xm' => '52494646',
  199. 'avi' => '52494646',
  200. 'cda' ='avi' => '52494646',
  201. 'cda' =646464646 ',
  202. 'qcp'=> '52494646',
  203. 'rmi' => '52494646',
  204. 'wav' => '52494646',
  205. '帽子' => '58435000'
  206. 'rar' => '526172211A0700',
  207. 'ast' =>; '5343486C',
  208. 'shw'=>; '53484F57',
  209. 'cpi'=4546464> ,
  210. '坐' => '5374756666497420',
  211. 'sdr' => '534D415254445257',
  212. 'cnv' =>; '53514CC5257',
  213. 'cnv' =>; '53514CC4F4344E560 ',
  214. '訊息' => 'E310000100000000',
  215. 'uce' =>; '55434558',
  216. 'ufa'=>; '554641C6D2C1',
  217. 'ufa'=>; '554641C6D2C1',
  218. 'pch' ,
  219. 'ctl' => '56455253494F4E20',
  220. 'ws2' =>; '575332303030',
  221. 'lwp' =>; '576F726450726F',dr. ,
  222. '動物園' => '5A4F4F20',
  223. 'ecf' => '5B47656E6572616C',
  224. 'vcw' => '5B4D535643',
  225. 'dun' => '5B4D535643',
  226. 'dun'' => '56506,8
  227. '山姆' => '5B7665725D',
  228. 'cpx' => '5B57696E646F7773',
  229. 'cfg' =>; '5B666C7473696D2E',
  230. 'cbk'=>; '5F434153455F',
  231. 'arj' =>; '60EA',
  232. 'vhd'=>; '636F6E6563746978',; '636F6E6563746978',; '636F6E6563746978',; '636F6E6563746978','c0' ,
  233. 'p10' =>; '64000000',
  234. 'dex'=>; '6465780A30303900',
  235. 'dsw'=>; '64737766696C65','dbb' => '6C33336C',
  236. 'acd' =>; '72696666',
  237. '內存' => '727473703A2F2F',
  238. 'dmg'=> '78 ',
  239. 'lgc'=>; '7B0D0A6F20',
  240. 'lgd' => '7B0D0A6F20',
  241. 'pwi' =>; '7B5C707769',
  242. 'pwi' =>; '7B5C707769',
  243. 'Brtf' =>; '7B5C707769',
  244. 'Brtf'=746372746312746 ',
  245. 'psp' =>; '7E424B00',
  246. 'wab' => '9CCBCB8D1375D211',
  247. 'wpf' =>; '81CDAB',
  248. 'png' =>; 89504E470D0A1A0A',
  249. 'aw' =>; '8A0109000000E108',
  250. 'hap'=> '91334846',
  251. 'skr'=>; '99',
  252. 'pkr' =>; '9901',
  253. 'qdf'=>; 'AC9EBD8F0000',
  254. 'pwl' =>; 'E3828596',
  255. 'dcx' = > 'B168DE3A',
  256. 'tib' =>; 'B46E6844',
  257. 'acs' =>; 'C3ABCDAB',
  258. 'lbk' =>; 'C8007900',
  259. 'class' =>; 'CAFEBABE',
  260. 'dbx' => 'CFAD12FE',
  261. '點'=> 'D0CF11E0A1B11AE1',
  262. 'pps' => 'D0CF11E0A1B11AE1',
  263. > 'D0CF11E0A1B11AE1',
  264. 'xla' =>; 'D0CF11E0A1B11AE1',
  265. 'xls'=> 'D0CF11E0A1B11AE1',
  266. 'wiz' =>; 'D0CF11E0A1B11AE1',
  267. 'ac_' => 'D0CF11E0A1B11AE1',
  268. 'adp'=>; 'D0CF11E0A1B11AE1',
  269. '四月' =>; 'D0CF11E0A1B11AE1',
  270. 'mtw' =>; 'D0CF11E0A1B11AE1',
  271. 'opt'=>; 'D0CF11E0A1B11AE1',
  272. 'pub' =>; 'D0CF11E0A1B11AE1',
  273. 'rvt'=> 'D0CF11E0A1B11AE1',
  274. 'sou' =>; 'D0CF11E0A1B11AE1',
  275. 'spo' => 'D0CF11E0A1B11AE1',
  276. 'vsd'=> 'D0CF11E0A1B11AE1',
  277. 'wps' = 'D01 00 ',
  278. 'arl' =>; 'D42A',
  279. 'aut' => 'D42A',
  280. 'wmf' =>; 'D7CDC69A',
  281. 'efx' =>; 'DCFE',
  282. '一' => 'E4525C7B8CD8A74D',
  283. 'rpm' => 'EDABEEDB',
  284. 'gho' =>; 'FEEF',
  285. 'ghs' =>; 'FEEF',
  286. 'wp' =>; 'FF575043',
  287. 'wpd'=>; 'FF575043',
  288. 'wpg'=>; 'FF575043',
  289. 'wpp'=>; 'FF575043',
  290. 'wp5'=> 'FF575043',
  291. 'wp6'=>; 'FF575043',
  292. 'jfif'=>; 'FFD8FF',
  293. 'jpe' => 'FFD8FF',
  294. 'jpeg' =>; 'FFD8FF',
  295. 'jpg' => 'FFD8FF',
  296. 'mof' =>; 'FFFE23006C006900',
  297. 'ipa' =>; '504B03040A',
  298. );
  299. /**
  300. * 檢查檔案類型
  301. *
  302. * @param string $filePath 檔案路徑
  303. * @param string $fileExt 檔案副檔名
  304. *
  305. * @return boolean
  306. */
  307. 公共靜態函數驗證($filePath, $fileExt)
  308. {
  309. //檔案格式未知
  310. if (!isset(self::$_fileFormats[$fileExt]))
  311. {
  312. 回傳false;
  313. }
  314. $length = strlen(self::$_fileFormats[$fileExt]);
  315. $bin = self::_readFile($filePath, $length);
  316. $fileHead = @unpack("H{$length}", $bin);
  317. //判斷檔案頭
  318. if ( strtolower(self::$_fileFormats[$fileExt]) == $fileHead[1])
  319. {
  320. 回傳true;
  321. }
  322. 回傳false;
  323. }
  324. /****
  325. * 讀取檔案內容
  326. *
  327. * @param string $filePath 檔案路徑
  328. * @param integer $size
  329. *
  330. * @return string
  331. */
  332. 真空函數_readFile($filePath, $size)
  333. {
  334. $file = fopen($filePath, "rb");
  335. $bin = fread($file, $ size);
  336. fclose($file);
  337. return $bin;
  338. }
  339. }
  340. ?>
複製代碼
呼叫範例:

  1. require './class_filetypevalidation.php';
  2. //路徑檔
  3. $Path = "DD.php';
  4. //路徑檔
  5. $Path = "DD./file test.png";
  6. $x = FileTypeValidation::validation($filePath, 'zip');
  7. var_dump($x);
?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn