>  기사  >  백엔드 개발  >  파일 헤더를 기반으로 파일 형식을 확인하는 PHP 코드

파일 헤더를 기반으로 파일 형식을 확인하는 PHP 코드

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

사용 예:

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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.