Maison  >  Article  >  développement back-end  >  php根据文件头验证文件类型的代码

php根据文件头验证文件类型的代码

WBOY
WBOYoriginal
2016-07-25 09:00:441590parcourir
  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. '3gp5' => '0000001866747970',
  13. 'm4a' => '00000020667479704D3441',
  14. 'ico' => '00000100',
  15. 'spl' => '00000100',
  16. 'vob' => '000001BA',
  17. 'cur' => '00000200',
  18. 'wb2' => '00000200',
  19. 'wk1' => '0000020006040600',
  20. 'wk3' => '00001A0000100400',
  21. 'wk4' => '00001A0002100400',
  22. 'wk5' => '00001A0002100400',
  23. '123' => '00001A00051004',
  24. 'qxd' => '00004D4D585052',
  25. 'mdf' => '010F0000',
  26. 'tr1' => '0110',
  27. 'rgb' => '01DA01010003',
  28. 'drw' => '07',
  29. 'dss' => '02647373',
  30. 'dat' => 'A90D000000000000',
  31. 'db3' => '03',
  32. 'qph' => '03000000',
  33. 'adx' => '80000020031204',
  34. 'db4' => '04',
  35. 'n' => 'FFFE0000',
  36. 'a' => 'FFFE0000',
  37. 'skf' => '07534B46',
  38. 'dtd' => '0764743264647464',
  39. 'db' => 'D0CF11E0A1B11AE1',
  40. 'pcx' => '0A050101',
  41. 'mp' => '0CED',
  42. 'doc' => 'D0CF11E0A1B11AE1',
  43. 'nri' => '0E4E65726F49534F',
  44. 'wks' => 'FF00020004040554',
  45. 'pf' => '1100000053434341',
  46. 'ntf' => '4E49544630',
  47. 'nsf' => '4E45534D1A01',
  48. 'arc' => '41724301',
  49. 'pak' => '5041434B',
  50. 'eth' => '1A350100',
  51. 'mkv' => '1A45DFA393428288',
  52. 'ws' => '1D7D',
  53. 'gz' => '1F8B08',
  54. 'tar.z' => '1FA0',
  55. 'ain' => '2112',
  56. 'lib' => '213C617263683E0A',
  57. 'msi' => 'D0CF11E0A1B11AE1',
  58. 'vmdk' => '4B444D',
  59. 'dsp' => '23204D6963726F73',
  60. 'amr' => '2321414D52',
  61. 'hdr' => '49536328',
  62. 'sav' => '24464C3240282329',
  63. 'eps' => 'C5D0D3C6',
  64. 'pdf' => '25504446',
  65. 'fdf' => '25504446',
  66. 'hqx' => '2854686973206669',
  67. 'log' => '2A2A2A2020496E73',
  68. 'ivr' => '2E524543',
  69. 'rm' => '2E524D46',
  70. 'rmvb' => '2E524D46',
  71. 'ra' => '2E7261FD00',
  72. 'au' => '646E732E',
  73. 'cat' => '30',
  74. 'evt' => '300000004C664C65',
  75. 'asf' => '3026B2758E66CF11',
  76. 'wma' => '3026B2758E66CF11',
  77. 'wmv' => '3026B2758E66CF11',
  78. 'wri' => 'BE000000AB',
  79. '7z' => '377ABCAF271C',
  80. 'psd' => '38425053',
  81. 'sle' => '414376',
  82. 'asx' => '3C',
  83. 'xdr' => '3C',
  84. 'dci' => '3C21646F63747970',
  85. 'manifest' => '3C3F786D6C2076657273696F6E3D',
  86. 'xml' => '3C3F786D6C2076657273696F6E3D22312E30223F3E',
  87. 'msc' => 'D0CF11E0A1B11AE1',
  88. 'fm' => '3C4D616B65724669',
  89. 'mif' => '56657273696F6E20',
  90. 'gid' => '4C4E0200',
  91. 'hlp' => '4C4E0200',
  92. 'dwg' => '41433130',
  93. 'syw' => '414D594F',
  94. 'abi' => '414F4C494E444558',
  95. 'aby' => '414F4C4442',
  96. 'bag' => '414F4C2046656564',
  97. 'idx' => '5000000020000000',
  98. 'ind' => '414F4C494458',
  99. 'pfc' => '414F4C564D313030',
  100. 'org' => '414F4C564D313030',
  101. 'vcf' => '424547494E3A5643',
  102. 'bin' => '424C4932323351',
  103. 'bmp' => '424D',
  104. 'dib' => '424D',
  105. 'prc' => '424F4F4B4D4F4249',
  106. 'bz2' => '425A68',
  107. 'tar.bz2' => '425A68',
  108. 'tbz2' => '425A68',
  109. 'tb2' => '425A68',
  110. 'rtd' => '43232B44A4434DA5',
  111. 'cbd' => '434246494C45',
  112. 'iso' => '4344303031',
  113. 'clb' => '434F4D2B',
  114. 'cpt' => '43505446494C45',
  115. 'cru' => '43525553482076',
  116. 'swf' => '465753',
  117. 'ctf' => '436174616C6F6720',
  118. 'dms' => '444D5321',
  119. 'adf' => '5245564E554D3A2C',
  120. 'dvr' => '445644',
  121. 'ifo' => '445644',
  122. 'cdr' => '52494646',
  123. 'vcd' => '454E545259564344',
  124. 'mdi' => '4550',
  125. 'e01' => '4C5646090D0AFF00',
  126. 'evtx' => '456C6646696C6500',
  127. 'qbb' => '458600000600',
  128. 'cpe' => '464158434F564552',
  129. 'flv' => '464C56',
  130. 'aiff' => '464F524D00',
  131. 'eml' => '582D',
  132. 'gif' => '47494638',
  133. 'pat' => '47504154',
  134. 'gx2' => '475832',
  135. 'sh3' => '4848474231',
  136. 'tif' => '4D4D002B',
  137. 'tiff' => '4D4D002B',
  138. 'mp3' => '494433',
  139. 'koz' => '49443303000000',
  140. 'crw' => '49491A0000004845',
  141. 'cab' => '4D534346',
  142. 'lit' => '49544F4C49544C53',
  143. 'chi' => '49545346',
  144. 'chm' => '49545346',
  145. 'jar' => '5F27A889',
  146. 'jg' => '4A47040E000000',
  147. 'kgb' => '4B47425F61726368',
  148. 'shd' => '68490000',
  149. 'lnk' => '4C00000001140200',
  150. 'obj' => '80',
  151. 'pdb' => 'ACED000573720012',
  152. 'mar' => '4D41723000',
  153. 'dmp' => '504147454455',
  154. 'hdmp' => '4D444D5093A7',
  155. 'mls' => '4D563243',
  156. 'mmf' => '4D4D4D440000',
  157. 'nvram' => '4D52564E',
  158. 'ppz' => '4D534346',
  159. 'snp' => '4D534346',
  160. 'tlb' => '4D53465402000100',
  161. 'dvf' => '4D535F564F494345',
  162. 'msv' => '4D535F564F494345',
  163. 'mid' => '4D546864',
  164. 'midi' => '4D546864',
  165. 'dsn' => '4D56',
  166. 'com' => 'EB',
  167. 'dll' => '4D5A',
  168. 'drv' => '4D5A',
  169. 'exe' => '4D5A',
  170. 'pif' => '4D5A',
  171. 'qts' => '4D5A',
  172. 'qtx' => '4D5A',
  173. 'sys' => 'FFFFFFFF',
  174. 'acm' => '4D5A',
  175. 'ax' => '4D5A900003000000',
  176. 'cpl' => 'DCDC',
  177. 'fon' => '4D5A',
  178. 'ocx' => '4D5A',
  179. 'olb' => '4D5A',
  180. 'scr' => '4D5A',
  181. 'vbx' => '4D5A',
  182. 'vxd' => '4D5A',
  183. '386' => '4D5A',
  184. 'api' => '4D5A900003000000',
  185. 'flt' => '76323030332E3130',
  186. 'zap' => '4D5A90000300000004000000FFFF',
  187. 'sln' => '4D6963726F736F66742056697375616C',
  188. 'jnt' => '4E422A00',
  189. 'jtp' => '4E422A00',
  190. 'cod' => '4E616D653A20',
  191. 'dbf' => '4F504C4461746162',
  192. 'oga' => '4F67675300020000',
  193. 'ogg' => '4F67675300020000',
  194. 'ogv' => '4F67675300020000',
  195. 'ogx' => '4F67675300020000',
  196. 'dw4' => '4F7B',
  197. 'pgm' => '50350A',
  198. 'pax' => '504158',
  199. 'pgd' => '504750644D41494E',
  200. 'img' => 'EB3C902A',
  201. 'zip' => '504B0304140000',
  202. 'docx' => '504B030414000600',
  203. 'pptx' => '504B030414000600',
  204. 'xlsx' => '504B030414000600',
  205. 'kwd' => '504B0304',
  206. 'odt' => '504B0304',
  207. 'odp' => '504B0304',
  208. 'ott' => '504B0304',
  209. 'sxc' => '504B0304',
  210. 'sxd' => '504B0304',
  211. 'sxi' => '504B0304',
  212. 'sxw' => '504B0304',
  213. 'wmz' => '504B0304',
  214. 'xpi' => '504B0304',
  215. 'xps' => '504B0304',
  216. 'xpt' => '5850434F4D0A5479',
  217. 'grp' => '504D4343',
  218. 'qemu' => '514649',
  219. 'abd' => '5157205665722E20',
  220. 'qsd' => '5157205665722E20',
  221. 'reg' => 'FFFE',
  222. 'sud' => '52454745444954',
  223. 'ani' => '52494646',
  224. 'cmx' => '52494646',
  225. 'ds4' => '52494646',
  226. '4xm' => '52494646',
  227. 'avi' => '52494646',
  228. 'cda' => '52494646',
  229. 'qcp' => '52494646',
  230. 'rmi' => '52494646',
  231. 'wav' => '52494646',
  232. 'cap' => '58435000',
  233. 'rar' => '526172211A0700',
  234. 'ast' => '5343486C',
  235. 'shw' => '53484F57',
  236. 'cpi' => 'FF464F4E54',
  237. 'sit' => '5374756666497420',
  238. 'sdr' => '534D415254445257',
  239. 'cnv' => '53514C4F434F4E56',
  240. 'cal' => 'B5A2B0B3B3B0A5B5',
  241. 'info' => 'E310000100000000',
  242. 'uce' => '55434558',
  243. 'ufa' => '554641C6D2C1',
  244. 'pch' => '564350434830',
  245. 'ctl' => '56455253494F4E20',
  246. 'ws2' => '575332303030',
  247. 'lwp' => '576F726450726F',
  248. 'bdr' => '5854',
  249. 'zoo' => '5A4F4F20',
  250. 'ecf' => '5B47656E6572616C',
  251. 'vcw' => '5B4D535643',
  252. 'dun' => '5B50686F6E655D',
  253. 'sam' => '5B7665725D',
  254. 'cpx' => '5B57696E646F7773',
  255. 'cfg' => '5B666C7473696D2E',
  256. 'cas' => '5F434153455F',
  257. 'cbk' => '5F434153455F',
  258. 'arj' => '60EA',
  259. 'vhd' => '636F6E6563746978',
  260. 'csh' => '6375736800000002',
  261. 'p10' => '64000000',
  262. 'dex' => '6465780A30303900',
  263. 'dsw' => '64737766696C65',
  264. 'flac' => '664C614300000022',
  265. 'dbb' => '6C33336C',
  266. 'acd' => '72696666',
  267. 'ram' => '727473703A2F2F',
  268. 'dmg' => '78',
  269. 'lgc' => '7B0D0A6F20',
  270. 'lgd' => '7B0D0A6F20',
  271. 'pwi' => '7B5C707769',
  272. 'rtf' => '7B5C72746631',
  273. 'psp' => '7E424B00',
  274. 'wab' => '9CCBCB8D1375D211',
  275. 'wpf' => '81CDAB',
  276. 'png' => '89504E470D0A1A0A',
  277. 'aw' => '8A0109000000E108',
  278. 'hap' => '91334846',
  279. 'skr' => '9501',
  280. 'gpg' => '99',
  281. 'pkr' => '9901',
  282. 'qdf' => 'AC9EBD8F0000',
  283. 'pwl' => 'E3828596',
  284. 'dcx' => 'B168DE3A',
  285. 'tib' => 'B46E6844',
  286. 'acs' => 'C3ABCDAB',
  287. 'lbk' => 'C8007900',
  288. 'class' => 'CAFEBABE',
  289. 'dbx' => 'CFAD12FE',
  290. 'dot' => 'D0CF11E0A1B11AE1',
  291. 'pps' => 'D0CF11E0A1B11AE1',
  292. 'ppt' => 'D0CF11E0A1B11AE1',
  293. 'xla' => 'D0CF11E0A1B11AE1',
  294. 'xls' => 'D0CF11E0A1B11AE1',
  295. 'wiz' => 'D0CF11E0A1B11AE1',
  296. 'ac_' => 'D0CF11E0A1B11AE1',
  297. 'adp' => 'D0CF11E0A1B11AE1',
  298. 'apr' => 'D0CF11E0A1B11AE1',
  299. 'mtw' => 'D0CF11E0A1B11AE1',
  300. 'opt' => 'D0CF11E0A1B11AE1',
  301. 'pub' => 'D0CF11E0A1B11AE1',
  302. 'rvt' => 'D0CF11E0A1B11AE1',
  303. 'sou' => 'D0CF11E0A1B11AE1',
  304. 'spo' => 'D0CF11E0A1B11AE1',
  305. 'vsd' => 'D0CF11E0A1B11AE1',
  306. 'wps' => 'D0CF11E0A1B11AE1',
  307. 'ftr' => 'D20A0000',
  308. 'arl' => 'D42A',
  309. 'aut' => 'D42A',
  310. 'wmf' => 'D7CDC69A',
  311. 'efx' => 'DCFE',
  312. 'one' => 'E4525C7B8CD8A74D',
  313. 'rpm' => 'EDABEEDB',
  314. 'gho' => 'FEEF',
  315. 'ghs' => 'FEEF',
  316. 'wp' => 'FF575043',
  317. 'wpd' => 'FF575043',
  318. 'wpg' => 'FF575043',
  319. 'wpp' => 'FF575043',
  320. 'wp5' => 'FF575043',
  321. 'wp6' => 'FF575043',
  322. 'jfif' => 'FFD8FF',
  323. 'jpe' => 'FFD8FF',
  324. 'jpeg' => 'FFD8FF',
  325. 'jpg' => 'FFD8FF',
  326. 'mof' => 'FFFE23006C006900',
  327. 'ipa' => '504B03040A',
  328. );
  329. /**
  330. * 检查文件类型
  331. *
  332. * @param string $filePath 文件路径
  333. * @param string $fileExt 文件扩展名
  334. *
  335. * @return boolean
  336. */
  337. public static function validation($filePath, $fileExt)
  338. {
  339. // 文件格式未知
  340. if (!isset(self::$_fileFormats[$fileExt]))
  341. {
  342. return false;
  343. }
  344. $length = strlen(self::$_fileFormats[$fileExt]);
  345. $bin = self::_readFile($filePath, $length);
  346. $fileHead = @unpack("H{$length}", $bin);
  347. // 判断文件头
  348. if (strtolower(self::$_fileFormats[$fileExt]) == $fileHead[1])
  349. {
  350. return true;
  351. }
  352. return false;
  353. }
  354. /**
  355. * 读取文件内容
  356. *
  357. * @param string $filePath 文件路径
  358. * @param integer $size
  359. *
  360. * @return string
  361. */
  362. private function _readFile($filePath, $size)
  363. {
  364. $file = fopen($filePath, "rb");
  365. $bin = fread($file, $size);
  366. fclose($file);
  367. return $bin;
  368. }
  369. }
  370. ?>
复制代码

调用示例:

  1. require './class_filetypevalidation.php';
  2. // 文件路径
  3. $filePath = "D:/test.png";
  4. $x = FileTypeValidation::validation($filePath, 'zip');
  5. var_dump($x);
  6. ?>
复制代码


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn