ファイルヘッダーに基づいてファイルタイプを検証する PHP コード

WBOY
リリース: 2016-07-25 09:00:44
オリジナル
1622 人が閲覧しました
  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 ' ,
    '0000002066747970',
  12. '3gp5' => '0000001866747970',
  13. 'm4a' =>
  14. 'ico' =&g t; ,
  15. 'spl' => '00000100',
  16. 'vob' => '00000200',
  17. 'wk1' = > '0000020006040600',
  18. 'wk3' => '00001A0000100400',
  19. 'wk4' => '00001A000210 0400'、
  20. '123' => '00001A00051004'、
  21. 'qxd' => '00004D4D585052',
  22. 'mdf' => '0110',
  23. 'rgb' => 07',
  24. 'dss' => '02647373',
  25. 'dat' => '03',
  26. 'qph' => ' => '80000020031204',
  27. 'db4' => 'FFFE0000',
  28. 'a' => '、'dtd' => => 'D0CF11E0A1B11AE1',
  29. 'nri' => '0E4E65726F49534F',
  30. 'wks' => '1100000053434341 ',
  31. 'ntf' => ' 4E49544630',
  32. 'nsf' => '4E45534D1A01',
  33. 'arc' => '5041434B',
  34. 'mkt; ; '1A45DFA393428288',
  35. 'ws' => '1D7D',
  36. 'tar.z' => '2112' ,
  37. 'lib' => '213C617263683E0A',
  38. 'msi' => '4B444D',
  39. 'dsp' => 73' ,
  40. '午前中' = > '2321414D52',
  41. 'hdr' => '49536328',
  42. 'sav' => '25504446',
  43. 'hqx' => 9'、
  44. 'ログ' => '2A2A2A2020496E73',
  45. 'ivr' => '2E524D46',
  46. 'ra' => 2E7261FD00',
  47. 'au' => '646E732E',
  48. 'cat' => '300000004C664C65',
  49. 'asf' =>
  50. うわー' => '3026B2758E66CF11',
  51. 'wmv' => '3026B2758E66 CF11',
  52. 'wri' => 'BE000000AB',
  53. 'psd' = > ' 38425053',
  54. 'sle' => '414376',
  55. 'asx' => '3C',
  56. 'dci' => '3C21646F63747970',
  57. ' ' => '3C3F786D6C2076657273696F6E3D',
  58. 'xml' => '3C3F786D6C20 76657273696F6E3D22312E30223F3E',
  59. 'msc' = > 'D0CF11E0 A1B11AE1',
  60. 'fm' => '3C4D616B65724669',
  61. 'mif' => 56657273696F6E20',
  62. 'gid' => '4C4E0200',
  63. 'dwg' => '41433130',
  64. 'syw' => 「、
  65. 」アビ' => '414F4C494E444558',
  66. 'aby' => '414F4C2046656564',
  67. 'idx' =>
  68. 'ind' => '414F4C494458 ',
  69. 'pfc' => '414F4C564D313030',
  70. 'vcf' => '424547494E3A5643',
  71. 'bin' => C4932323351',
  72. 'bmp' => '424D',
  73. 'dib' => '424F4F4B4D4F4249',
  74. 'tar.bz2' => 425A68',
  75. 'tbz2' => '425A68',
  76. 'rtd' => '43232B44A4434DA5',
  77. 'cbd' =>
  78. 'イソ' => '4344303031',
  79. 'clb' => '43505446494C45',
  80. 'cru' => t; '465753 ',
  81. 'ctf' => '436174616C6F6720',
  82. 'dms' => '5245564E554D3A2C',
  83. 'dvr' =>
  84. 'ifo ' => '445644',
  85. 'cdr' => '454E545259564344',
  86. 'mdi' => '4C56460 90D0AFF00 ',
  87. 'evtx' => '456C6646696C6500',
  88. 'qbb' => '45860 0000600',
  89. ' cpe' => '464C5 6'、
  90. 'あふ' => '464F524D00',
  91. 'eml' => '47494638',
  92. 'gx2' => ',
  93. 'sh3' => '4848474231',
  94. 'tif' => '= & gt;' 4d4d002b ',
  95. 'mp3' = & gt; koz '= & gt;' 4944330000000 ',
  96. 'crw' = & gt; ',
  97. 'lit' => '49545346',
  98. 』 chm' => '49545346',
  99. 'jar' => '5F27A889',
  100. 'jg' => '4A47040E000000',
  101. 'kgb' => '4B47425F61726368',
  102. 'shd' => '68490000',
  103. 'lnk' => '4C00000001140200',
  104. 'obj' => '80',
  105. 'pdb' => 'ACED000573720012',
  106. 'mar' => '4D41723000',
  107. 'dmp' => '504147454455',
  108. 'hdmp' => '4D444D5093A7',
  109. 'mls' => '4D563243',
  110. 'mmf' => '4D4D4D440000',
  111. 'nvram' => '4D52564E',
  112. 'ppz' => '4D534346',
  113. 'snp' => '4D534346',
  114. 'tlb' => '4D53465402000100',
  115. 'dvf' => '4D535F564F494345',
  116. 'msv' => '4D535F564F494345',
  117. 'mid' => '4D546864',
  118. 'midi' => '4D546864',
  119. 'dsn' => '4D56',
  120. 'com' => 'EB',
  121. 'dll' => '4D5A',
  122. 'drv' => '4D5A',
  123. 'exe' => '4D5A',
  124. 'pif' => '4D5A',
  125. 'qts' => '4D5A',
  126. 'qtx' => '4D5A',
  127. 'sys' => 'FFFFFFFF',
  128. 'acm' => '4D5A',
  129. 'ax' => '4D5A900003000000',
  130. 'cpl' => 'DCDC',
  131. 'fon' => '4D5A',
  132. 'ocx' => '4D5A',
  133. 'olb' => '4D5A',
  134. 'scr' => '4D5A',
  135. 'vbx' => '4D5A',
  136. 'vxd' => '4D5A',
  137. '386' => '4D5A',
  138. 'api' => '4D5A900003000000',
  139. 'flt' => '76323030332E3130',
  140. 'ザップ' => '4D5A90000300000004000000FFFF',
  141. 'sln' => '4D6963726F736F66742056697375616C',
  142. 'jnt' => '4E422A00',
  143. 'jtp' => '4E422A00',
  144. 'タラ' => '4E616D653A20',
  145. 'dbf' => '4F504C4461746162',
  146. 'oga' => '4F67675300020000',
  147. 'ogg' => '4F67675300020000',
  148. 'ogv' => '4F67675300020000',
  149. 'ogx' => '4F67675300020000',
  150. 'dw4' => '4F7B',
  151. 'pgm' => '50350A',
  152. 'pax' => '504158',
  153. 'pgd' => '504750644D41494E',
  154. 'img' => 'EB3C902A',
  155. 'zip' => '504B0304140000',
  156. 'docx' => '504B030414000600',
  157. 'pptx' => '504B030414000600',
  158. 'xlsx' => '504B030414000600',
  159. 'kwd' => '504B0304',
  160. 'odt' => '504B0304',
  161. 'odp' => '504B0304',
  162. 'ott' => '504B0304',
  163. 'sxc' => '504B0304',
  164. 'sxd' => '504B0304',
  165. 'sxi' => '504B0304',
  166. 'sxw' => '504B0304',
  167. 'wmz' => '504B0304',
  168. 'xpi' => '504B0304',
  169. 'xps' => '504B0304',
  170. 'xpt' => '5850434F4D0A5479',
  171. 'grp' => '504D4343',
  172. 'qemu' => '514649',
  173. 'abd' => '5157205665722E20',
  174. 'qsd' => '5157205665722E20',
  175. 'reg' => 'FFFE',
  176. 'sud' => '52454745444954',
  177. 'アニ' => '52494646',
  178. 'cmx' => '52494646',
  179. 'ds4' => '52494646',
  180. '4xm' => '52494646',
  181. 'avi' => '52494646',
  182. 'cda' => '52494646',
  183. 'qcp' => '52494646',
  184. 'rmi' => '52494646',
  185. 'wav' => '52494646',
  186. 'キャップ' => '58435000',
  187. 'rar' => '526172211A0700',
  188. 'ast' => '5343486C',
  189. 'shw' => '53484F57',
  190. 'cpi' => 'FF464F4E54',
  191. '座る' => '5374756666497420',
  192. 'sdr' => '534D415254445257',
  193. 'cnv' => '53514C4F434F4E56',
  194. 'cal' => 'B5A2B0B3B3B0A5B5',
  195. '情報' => 'E310000100000000',
  196. 'uce' => '55434558',
  197. 'ufa' => '554641C6D2C1',
  198. 'pch' => '564350434830',
  199. 'ctl' => '56455253494F4E20',
  200. 'ws2' => '575332303030',
  201. 'lwp' => '576F726450726F',
  202. 'bdr' => '5854',
  203. '動物園' => '5A4F4F20',
  204. 'ecf' => '5B47656E6572616C',
  205. 'vcw' => '5B4D535643',
  206. 'dun' => '5B50686F6E655D',
  207. 'サム' => '5B7665725D',
  208. 'cpx' => '5B57696E646F7773',
  209. 'cfg' => '5B666C7473696D2E',
  210. 'cas' => '5F434153455F',
  211. 'cbk' => '5F434153455F',
  212. 'arj' => '60EA',
  213. 'vhd' => '636F6E6563746978',
  214. 'csh' => '6375736800000002',
  215. 'p10' => '64000000',
  216. 'dex' => '6465780A30303900',
  217. 'dsw' => '64737766696C65',
  218. 'flac' => '664C614300000022',
  219. 'dbb' => '6C33336C',
  220. 'acd' => '72696666',
  221. 'ラム' => '727473703A2F2F',
  222. 'dmg' => '78',
  223. 'lgc' => '7B0D0A6F20',
  224. 'lgd' => '7B0D0A6F20',
  225. 'pwi' => '7B5C707769',
  226. 'rtf' => '7B5C72746631',
  227. 'psp' => '7E424B00',
  228. 'ワブ' => '9CCBCB8D1375D211',
  229. 'wpf' => '81CDAB',
  230. 'png' => '89504E470D0A1A0A',
  231. 'aw' => '8A0109000000E108',
  232. 'hap' => '91334846',
  233. 'skr' => '9501',
  234. 'gpg' => '99',
  235. 'pkr' => '9901',
  236. 'qdf' => 'AC9EBD8F0000',
  237. 'pwl' => 'E3828596',
  238. 'dcx' => 'B168DE3A',
  239. 'tib' => 'B46E6844',
  240. 'acs' => 'C3ABCDAB',
  241. 'lbk' => 'C8007900',
  242. 'クラス' => 'カフェベイブ',
  243. 'dbx' => 'CFAD12FE',
  244. 'ドット' => 'D0CF11E0A1B11AE1',
  245. 'pps' => 'D0CF11E0A1B11AE1',
  246. 'ppt' => 'D0CF11E0A1B11AE1',
  247. 'xla' => 「D0CF11E0A1B11AE1」、
  248. 'xls' => 'D0CF11E0A1B11AE1',
  249. 'wiz' => 'D0CF11E0A1B11AE1',
  250. 'ac_' => 'D0CF11E0A1B11AE1',
  251. 'adp' => 'D0CF11E0A1B11AE1',
  252. 'apr' => 'D0CF11E0A1B11AE1',
  253. 'mtw' => 'D0CF11E0A1B11AE1',
  254. 'opt' => 'D0CF11E0A1B11AE1',
  255. 'パブ' => 'D0CF11E0A1B11AE1',
  256. 'rvt' => 'D0CF11E0A1B11AE1',
  257. 'sou' => 'D0CF11E0A1B11AE1',
  258. 'spo' => 'D0CF11E0A1B11AE1',
  259. 'vsd' => 'D0CF11E0A1B11AE1',
  260. 'wps' => 'D0CF11E0A1B11AE1',
  261. 'ftr' => 'D20A0000',
  262. 'arl' => 'D42A',
  263. 'aut' => 'D42A',
  264. 'wmf' => 'D7CDC69A',
  265. 'efx' => 'DCFE',
  266. 'one' => 'E4525C7B8CD8A74D',
  267. 'rpm' => 'EDABEEDB',
  268. 'gho' => 'FEEF',
  269. 'ghs' => 'FEEF',
  270. 'wp' => 'FF575043',
  271. 'wpd' => 'FF575043',
  272. 'wpg' => 'FF575043',
  273. 'wpp' => 'FF575043',
  274. 'wp5' => 'FF575043',
  275. 'wp6' => 'FF575043',
  276. 'jfif' => 'FFD8FF',
  277. 'jpe' => 'FFD8FF',
  278. 'jpeg' => 'FFD8FF',
  279. 'jpg' => 'FFD8FF',
  280. 'mof' => 'FFFE23006C006900',
  281. 'ipa' => '504B03040A',
  282. );
  283. /**
  284. * ファイルタイプをチェック
  285. *
  286. * @param string $filePath ファイルパス
  287. * @param string $fileExt ファイル拡張子
  288. *
  289. * @return boolean
  290. */
  291. public static function validation($filePath, $fileExt)
  292. {
  293. // 文件格式不明
  294. if (!isset(self::$_fileFormats[$fileExt ]))
  295. {
  296. return false;
  297. }
  298. $length = strlen(self::$_fileFormats[$fileExt]);
  299. $bin = self::_readFile($filePath, $length);
  300. $fileHead = @unpack ("H{$length}", $bin);
  301. // 判断文头
  302. if (strto lower(self::$_fileFormats[$fileExt]) == $fileHead[1])
  303. {
  304. return true;
  305. }
  306. return false;
  307. }
  308. /**
  309. * ファイルの内容を読み取ります
  310. *
  311. * @param string $filePath ファイルパス
  312. * @param integer $size
  313. *
  314. * @return string
  315. */
  316. private function _readFile($filePath, $size)
  317. {
  318. $file = fopen($filePath, "rb");
  319. $bin = fread($file , $size);
  320. fclose($file);
  321. return $bin;
  322. }
  323. }
  324. ?>
复制代

调用例:

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


ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート