Php verifies the file type code based on the file header

Source: Internet
Author: User
Php verifies the file type code based on the file header

  1. /**
  2. * Document type
  3. *
  4. * @ Author Silver
  5. * By bbs.it-home.org
  6. */
  7. Class FileTypeValidation
  8. {
  9. // File type, different header information
  10. Private static $ _ fileFormats = Array (
  11. 'Jp2' => '0000000c6a502020 ',
    '3gp '=> '123 ',
  12. & Apos; 3gp5 & apos; = & apos; 0000001866747970 & apos ',
  13. 'M4a '=> '00000020667479704d3441 ',
  14. 'Ico '=> '123 ',
  15. 'Spl' => '123 ',
  16. 'Vob' => '0001ba ',
  17. & Apos; cur & apos; = & apos; 00000200 & apos ',
  18. 'Wb2' => '123 ',
  19. 'Wk1 '=> '123 ',
  20. 'Wk3' => '20171a0000100400 ',
  21. 'Wk4 '=> '20171a0002100400 ',
  22. 'Wk5 '=> '20171a0002100400 ',
  23. '20140901' => '20171a00051004 ',
  24. 'Qxd '=> '20174d4d585052 ',
  25. 'Mdf '=> '010f0000 ',
  26. 'Tr1' => '123 ',
  27. 'Rgb '=> '01da01010003 ',
  28. 'Drw' => '07 ',
  29. 'Dss '=> '123 ',
  30. 'Dat '=> 'a90d000000000000 ',
  31. 'Db3' => '03 ',
  32. 'Qph' => '123 ',
  33. 'Adx' => '123 ',
  34. 'Db4' => '04 ',
  35. 'N' => 'fffe000000 ',
  36. 'A' => 'fffe000000 ',
  37. 'Skf '=> '07534b46 ',
  38. 'Dtd '=> '123 ',
  39. 'DB' => 'd0cf11e0a1b11ae1 ',
  40. 'Pcx' => '0a050101 ',
  41. 'MP' => '0ced ',
  42. 'Doc' => 'd0cf11e0a1b11ae1 ',
  43. 'Nri' => '0e4e65108f49534f ',
  44. 'Wk' => 'ff00020004040554 ',
  45. 'Pf '=> '123 ',
  46. 'Ntf' => '4e49544630 ',
  47. 'NSF '=> '4e45534d1a01 ',
  48. 'Arc' => '123 ',
  49. 'Pak '=> '5041434b ',
  50. 'Eth '=> '1a350100 ',
  51. 'Mkv '=> '1a45dfa393428288 ',
  52. 'Ws '=> '1d7d ',
  53. 'Gz '=> '1f8b08 ',
  54. 'Tar. Z' => '1fa0 ',
  55. 'Ain '=> '123 ',
  56. 'Lib' => 'Export c617264243e0a ',
  57. 'Msi '=> 'd0cf11e0a1b11ae1 ',
  58. 'Vmdk' => '4b444d ',
  59. 'Dsps '=> '23204d6963726f73 ',
  60. 'Amr' => '2321414d52 ',
  61. & Apos; hdr & apos; = & apos; 49536328 & apos ',
  62. 'Sav' => '24464c3240282329 ',
  63. 'Eps '=> 'c5d0d3c6 ',
  64. 'PDF '=> '123 ',
  65. 'Fdf '=> '123 ',
  66. 'Hqx' => '123 ',
  67. 'Log' => '2a2a2a2020496e73 ',
  68. 'Ivr' => '2e524543 ',
  69. 'Rm '=> '2e524d46 ',
  70. 'Rmvb '=> '2e524d46 ',
  71. 'A' => '2e00001fd00 ',
  72. 'Au '=> '646e732e ',
  73. 'Cat' => '30 ',
  74. 'Evt' => '300000004c664c65 ',
  75. 'Asf' => '3026b2758e66cf11 ',
  76. 'Wma' => '3026b2758e66cf11 ',
  77. 'Wmv '=> '3026b2758e66cf11 ',
  78. 'Wri' => 'bedomain00ab ',
  79. '7z' => '377abcaf271c ',
  80. 'Psd '=> '123 ',
  81. 'Sync' => '123 ',
  82. 'Asx' => '3C ',
  83. 'Xdr '=> '3C ',
  84. 'Dci' => '3c21646f63747970 ',
  85. 'Manifest '=> '3c3f786d6c2076657272136f6e3d ',
  86. 'Xml' => '3c3f786d6c2076657272136f6e3d22312e30223f3e ',
  87. 'Msc '=> 'd0cf11e0a1b11ae1 ',
  88. 'FM '=> '3c4d616b65724669 ',
  89. 'If' => '56657272136f6e20 ',
  90. 'Gid' => '4c4e0200 ',
  91. 'Hlp '=> '4c4e0200 ',
  92. 'Dwg '=> '123 ',
  93. 'Syw' => '414d594f ',
  94. 'Abi '=> '414f4c494e444558 ',
  95. 'Aby' => '414f4c4442 ',
  96. 'Bag' => '414f4c2046656564 ',
  97. 'Idx' => '123 ',
  98. 'IND '=> '414f4c494458 ',
  99. 'Pfc '=> '414f4c564d313030 ',
  100. 'Org '=> '414f4c564d313030 ',
  101. 'Vcf '=> '2017547494e3a5643 ',
  102. 'Bin' => 'Export c4932323351 ',
  103. 'Bmp '=> 'mongod ',
  104. 'Dib' => 'did ',
  105. 'Prc' => '0000f4f4b4d4f00009 ',
  106. 'Bz2' => '2017a68 ',
  107. 'Tar.bz2 '=> '2017a68 ',
  108. 'Tbz2' => '0000a68 ',
  109. 'Tb2' => '2017a68 ',
  110. 'Rtd' => '43232b44a4434da5 ',
  111. 'CBD '=> '434246494c45 ',
  112. 'ISO '=> '123 ',
  113. 'Clb' => '434f4d2b ',
  114. 'Cpt' => '43505446494c45 ',
  115. 'Cru' => '000000 ',
  116. 'Swf '=> '123 ',
  117. 'Ctf' => '436174616c6f6720 ',
  118. 'DMs' => '444d5321 ',
  119. 'Adf' => '5245564e554d3a2c ',
  120. 'DVR' => '123 ',
  121. 'Ifo' => '123 ',
  122. 'Cdrs '=> '123 ',
  123. 'Vcd' => '454e545259564344 ',
  124. 'Mdi '=> '123 ',
  125. 'E01' => '4c5646090d0aff00 ',
  126. 'Evtx '=> '456c6646696c6500 ',
  127. 'Qbb' => '123 ',
  128. 'Cpe '=> '464158434f564552 ',
  129. 'Flv' => '464c56 ',
  130. 'Aiff '=> '464f524d00 ',
  131. 'Yml' => '582d ',
  132. 'Gif' => '123 ',
  133. 'Pat '=> '123 ',
  134. 'Gx2 '=> '123 ',
  135. 'Sh3' => '123 ',
  136. 'Tif '=> '4d4d002b ',
  137. 'Tiff '=> '4d4d002b ',
  138. 'Mp3' => '123 ',
  139. 'Koz' => '123 ',
  140. 'Crw' => '49491a0000004845 ',
  141. 'Cab '=> '4d534346 ',
  142. 'Command' => '49544f4c49544c53 ',
  143. 'Chi '=> '123 ',
  144. 'Chm' => '123 ',
  145. 'Jar' => '5f27a889 ',
  146. 'JG' => '4a47040e000000 ',
  147. 'K' => '4b47213f61726368 ',
  148. 'Shd' => '123 ',
  149. 'Lnk '=> '4c00000001140200 ',
  150. 'Obj '=> '80 ',
  151. 'Pdb' => 'aced000573720012 ',
  152. 'Mar '=> '4d000023000 ',
  153. 'Dmp '=> '123 ',
  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. '000000' => '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. 'Ogy' => '4f67675300020000 ',
  194. 'Ogv' => '4f67675300020000 ',
  195. 'Ogx' => '4f67675300020000 ',
  196. 'Dw4' => '4f7b ',
  197. 'Pgm '=> '50350a ',
  198. 'Pax' => '123 ',
  199. 'Pgd '=> '504750644d41494e ',
  200. 'IMG '=> 'eb3c902a ',
  201. 'Zip' => '504b0304140000 ',
  202. 'Docx' => '504b030414000600 ',
  203. 'Ppt' => '504b030414000600 ',
  204. 'Xlsx' => '504b030414000600 ',
  205. 'Kwd '=> '504b0304 ',
  206. 'Odt' => '504b0304 ',
  207. 'Odp' => '504b0304 ',
  208. 'Ott '=> '504b0304 ',
  209. 'Sxc '=> '504b0304 ',
  210. 'Sxd '=> '504b0304 ',
  211. 'Ssi' => '504b0304 ',
  212. 'Sxw' => '504b0304 ',
  213. 'Wmz' => '504b0304 ',
  214. 'Xpi '=> '504b0304 ',
  215. 'Xps '=> '504b0304 ',
  216. 'Xpt' => '5850434f4d0a5479 ',
  217. 'Grp '=> '504d4343 ',
  218. 'Qemu' => '123 ',
  219. 'Abd' => '5157205665722e20 ',
  220. 'Qsd' => '5157205665722e20 ',
  221. 'Reg '=> 'fffe ',
  222. 'Sud' => '123 ',
  223. 'Ani '=> '123 ',
  224. 'Cmx' => '20140901 ',
  225. 'Ds4' => '123 ',
  226. '4xm '=> '123 ',
  227. 'Av' => '123 ',
  228. 'Cda' => '123 ',
  229. 'Qcp' => '123 ',
  230. 'Rummi' => '123 ',
  231. 'Wav '=> '123 ',
  232. 'Cap' => '123 ',
  233. 'Rar '=> '526172211a0700 ',
  234. 'Ast '=> '5343486c ',
  235. 'Shw' => '53484f57 ',
  236. 'CPI '=> 'ff464f4e54 ',
  237. 'Sit '=> '123 ',
  238. 'Sdr '=> '534d2017254445257 ',
  239. 'Fcn' => '53514c4f434f4e56 ',
  240. 'Cal '=> 'b5a2b0b3b0a5b5 ',
  241. 'Info' => 'e31000000000000 ',
  242. 'Uce '=> '123 ',
  243. 'Ufa '=> '554641c6d2c1 ',
  244. 'Pch' => '123 ',
  245. 'Ctl '=> '56455253494f4e20 ',
  246. 'Ws2 '=> '123 ',
  247. 'Lwp '=> '576f000000000000f ',
  248. 'Bdr '=> '123 ',
  249. 'Zoo' => '5a4f4f20 ',
  250. 'Ecf' => '5b47656e6538516c ',
  251. 'Vcw' => '5b4d535643 ',
  252. 'Dun' => '5b50686f6e655d ',
  253. 'Sam '=> '5b7665725d ',
  254. 'Cpx' => '5b57696e646f7773 ',
  255. 'Cfg '=> '5b666c7471_6d2e ',
  256. 'Cas '=> '5f434153455f ',
  257. 'Cbk' => '5f434153455f ',
  258. 'Arj' => '60ea ',
  259. 'Vhd '=> '636f6e6563746978 ',
  260. 'CSH' => '123 ',
  261. 'P10' => '123 ',
  262. 'Dex '=> '6465780a30303900 ',
  263. 'Dsw' => '64737766696c65 ',
  264. 'Flac '=> '664c614300000022 ',
  265. 'Dbb' => '6c33336c ',
  266. 'ACD' => '123 ',
  267. 'Ram' => '727473703a2f2f ',
  268. 'Dmg '=> '78 ',
  269. 'Lgc '=> '7b0d0a8f20 ',
  270. 'Lgd '=> '7b0d0a6f20 ',
  271. 'Pwi '=> '7b5c707769 ',
  272. 'Rtf '=> '7b5c72746631 ',
  273. 'PSP '=> '7e0000b00 ',
  274. 'Wab' => '9ccbcb8d1375d211 ',
  275. 'Wpf '=> '81cdab ',
  276. 'PNG '=> '89504e470d0a1a0a ',
  277. 'Aw' => '8a01090020.e108 ',
  278. 'Ha' => '123 ',
  279. 'Skr' => '123 ',
  280. 'Gpg' => '99 ',
  281. 'Pkr' => '123 ',
  282. 'Qdf '=> 'ac9ebd8f0000 ',
  283. 'Pwl' => 'e3828596 ',
  284. 'Dcx' => 'b168de3a ',
  285. 'Tid' => '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. 'Rdv' => 'd0cf11e0a1b11ae1 ',
  303. 'Sou' => 'd0cf11e0a1b11ae1 ',
  304. 'SPO' => 'd0cf11e0a1b11ae1 ',
  305. 'Password' => 'd0cf11e0a1b11ae1 ',
  306. 'Wps' => 'd0cf11e0a1b11ae1 ',
  307. 'Ftr' => 'd20a0000 ',
  308. 'Arl' => 'd42a ',
  309. 'Aut' => 'd42a ',
  310. 'WMF '=> 'd7csc69a ',
  311. 'Efx' => 'dcfe ',
  312. 'One' => 'e4525c7b8cd8a74d ',
  313. 'RP' => '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. * Check the file type
  331. *
  332. * @ Param string $ filePath file path
  333. * @ Param string $ fileExt file extension
  334. *
  335. * @ Return boolean
  336. */
  337. Public static function validation ($ filePath, $ fileExt)
  338. {
  339. // Unknown file format
  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. // Determine the file header
  348. If (strtolower (self: $ _ fileFormats [$ fileExt]) = $ fileHead [1])
  349. {
  350. Return true;
  351. }
  352. Return false;
  353. }
  354. /**
  355. * Reading file content
  356. *
  357. * @ Param string $ filePath file path
  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. ?>

Call example:

  1. Require './class_filetypevalidation.php ';
  2. // File path
  3. $ FilePath = "D:/test.png ";
  4. $ X = FileTypeValidation: validation ($ filePath, 'Zip ');
  5. Var_dump ($ x );
  6. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.