Rumah > pembangunan bahagian belakang > tutorial php > php基于文件头信息检测文件类型的代码

php基于文件头信息检测文件类型的代码

WBOY
Lepaskan: 2016-07-25 09:00:40
asal
1068 orang telah melayarinya
  1. //检测文件类型

  2. function checkFileType($fileName){
  3. $file = fopen($fileName, "rb");
  4. $bin = fread($file, 2); //只读2字节
  5. fclose($file);
  6. // C为无符号整数,网上搜到的都是c,为有符号整数,这样会产生负数判断不正常
  7. $strInfo = @unpack("C2chars", $bin);
  8. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  9. $fileType = '';
  10. switch( $typeCode )

  11. {
  12. case '255216':
  13. return $typeCode. ' : ' .'jpg';
  14. break;
  15. case '7173':
  16. return $typeCode. ' : ' .'gif';
  17. break;
  18. case '13780':
  19. return $typeCode. ' : ' .'png';
  20. break;
  21. case '6677':
  22. return $typeCode. ' : ' .'bmp';
  23. break;
  24. case '7790':
  25. return $typeCode. ' : ' .'exe';
  26. break;
  27. case '7784':
  28. return $typeCode. ' : ' .'midi';
  29. break;
  30. case '8297':
  31. return $typeCode. ' : ' .'rar';
  32. break;
  33. default:
  34. return $typeCode. ' : ' .'Unknown';
  35. break;
  36. }
  37. //return $typeCode;
  38. }
  39. $file_name = '11.doc';

  40. echo checkFileType($file_name);
  41. ?>
复制代码

2、根据php 文件头信息验证文件类型的类

  1. /*通过文件名,获得文件类型*

  2. *$filename="d:/1.png";echo cFileTypeCheck::getFileType($filename); 打印:png
  3. */
  4. class cFileTypeCheck
  5. {
  6. private static $_TypeList=array();
  7. private static $CheckClass=null;
  8. private function __construct($filename)
  9. {
  10. self::$_TypeList=$this->getTypeList();
  11. }
  12. /**
  13. *处理文件类型映射关系表*
  14. *
  15. * @param string $filename 文件类型
  16. * @return string 文件类型,没有找到返回:other
  17. */
  18. private function _getFileType($filename)
  19. {
  20. $filetype="other";
  21. if(!file_exists($filename)) throw new Exception("no found file!");
  22. $file = @fopen($filename,"rb");
  23. if(!$file) throw new Exception("file refuse!");
  24. $bin = fread($file, 15); //只读15字节 各个不同文件类型,头信息不一样。
  25. fclose($file);
  26. $typelist=self::$_TypeList;
  27. foreach ($typelist as $v)
  28. {
  29. $blen=strlen(pack("H*",$v[0])); //得到文件头标记字节数
  30. $tbin=substr($bin,0,intval($blen)); ///需要比较文件头长度
  31. if(strtolower($v[0])==strtolower(array_shift(unpack("H*",$tbin))))
  32. {
  33. return $v[1];
  34. }
  35. }
  36. return $filetype;
  37. }
  38. /**
  39. *得到文件头与文件类型映射表*
  40. *
  41. * @return array array(array('key',value)...)
  42. */
  43. public function getTypeList()
  44. {
  45. return array(array("FFD8FFE1","jpg"),
  46. array("89504E47","png"),
  47. array("47494638","gif"),
  48. array("49492A00","tif"),
  49. array("424D","bmp"),
  50. array("41433130","dwg"),
  51. array("38425053","psd"),
  52. array("7B5C727466","rtf"),
  53. array("3C3F786D6C","xml"),
  54. array("68746D6C3E","html"),
  55. array("44656C69766572792D646174","eml"),
  56. array("CFAD12FEC5FD746F","dbx"),
  57. array("2142444E","pst"),
  58. array("D0CF11E0","xls/doc"),
  59. array("5374616E64617264204A","mdb"),
  60. array("FF575043","wpd"),
  61. array("252150532D41646F6265","eps/ps"),
  62. array("255044462D312E","pdf"),
  63. array("E3828596","pwl"),
  64. array("504B0304","zip"),
  65. array("52617221","rar"),
  66. array("57415645","wav"),
  67. array("41564920","avi"),
  68. array("2E7261FD","ram"),
  69. array("2E524D46","rm"),
  70. array("000001BA","mpg"),
  71. array("000001B3","mpg"),
  72. array("6D6F6F76","mov"),
  73. array("3026B2758E66CF11","asf"),
  74. array("4D546864","mid"));
  75. }
  76. public static function getFileType($filename)
  77. {
  78. if(!self::$CheckClass) self::$CheckClass=new self($filename);
  79. $class=self::$CheckClass;
  80. return $class->_getFileType($filename);
  81. }
  82. }
  83. $filename="22.jpg";

  84. echo $filename,"t",cFileTypeCheck::getFileType($filename),"rn";
  85. $filename="11.doc";
  86. echo $filename,"t",cFileTypeCheck::getFileType($filename),"rn";
  87. //也可以这样检测

  88. $filename = '22.jpg';
  89. $extname = strtolower(substr($filename, strrpos($filename, '.') + 1));
  90. echo $extname.'
    ';
  91. $file = @fopen($filename, 'rb');
  92. if ($file)
  93. {
  94. $str = @fread($file, 0x400); // 读取前 1024 个字节
  95. echo substr($str, 0, 4);
  96. @fclose($file);
  97. }
  98. if (substr($str, 0, 4) == 'MThd' && $extname != 'txt')
  99. {
  100. $format = 'mid';
  101. }
  102. elseif (substr($str, 0, 4) == 'RIFF' && $extname == 'wav')
  103. {
  104. $format = 'wav';
  105. }
  106. elseif (substr($str ,0, 3) == "/xFF/xD8/xFF")
  107. {
  108. $format = 'jpg';
  109. }
  110. elseif (substr($str ,0, 4) == 'GIF8' && $extname != 'txt')
  111. {
  112. $format = 'gif';
  113. }
  114. elseif (substr($str ,0, 8 ) == "/x89/x50/x4E/x47/x0D/x0A/x1A/x0A")
  115. {
  116. $format = 'png';
  117. }
  118. elseif (substr($str ,0, 2) == 'BM' && $extname != 'txt')
  119. {
  120. $format = 'bmp';
  121. }
  122. elseif ((substr($str ,0, 3) == 'CWS' || substr($str ,0, 3) == 'FWS') && $extname != 'txt')
  123. {
  124. $format = 'swf';
  125. }
  126. elseif (substr($str ,0, 4) == "/xD0/xCF/x11/xE0")
  127. { // D0CF11E == DOCFILE == Microsoft Office Document
  128. if (substr($str,0x200,4) == "/xEC/xA5/xC1/x00" || $extname == 'doc')
  129. {
  130. $format = 'doc';
  131. }
  132. elseif (substr($str,0x200,2) == "/x09/x08" || $extname == 'xls')
  133. {
  134. $format = 'xls';
  135. } elseif (substr($str,0x200,4) == "/xFD/xFF/xFF/xFF" || $extname == 'ppt')
  136. {
  137. $format = 'ppt';
  138. }
  139. } elseif (substr($str ,0, 4) == "PK/x03/x04")
  140. {
  141. $format = 'zip';
  142. } elseif (substr($str ,0, 4) == 'Rar!' && $extname != 'txt')
  143. {
  144. $format = 'rar';
  145. } elseif (substr($str ,0, 4) == "/x25PDF")
  146. {
  147. $format = 'pdf';
  148. } elseif (substr($str ,0, 3) == "/x30/x82/x0A")
  149. {
  150. $format = 'cert';
  151. } elseif (substr($str ,0, 4) == 'ITSF' && $extname != 'txt')
  152. {
  153. $format = 'chm';
  154. } elseif (substr($str ,0, 4) == "/x2ERMF")
  155. {
  156. $format = 'rm';
  157. } elseif ($extname == 'sql')
  158. {
  159. $format = 'sql';
  160. } elseif ($extname == 'txt')
  161. {
  162. $format = 'txt';
  163. }
  164. echo $format;
  165. ?>
复制代码


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan