首頁 >後端開發 >php教程 >php基於文件頭資訊檢測文件類型的程式碼

php基於文件頭資訊檢測文件類型的程式碼

WBOY
WBOY原創
2016-07-25 09:00:401067瀏覽
  1. //偵測檔案類型

  2. function checkFileType($fileName){ $file = fopen( $fileName, "rb");
  3. $bin = fread($file, 2); //只讀2位元組
  4. fclose($file);
  5. // C為無符號整數,網路上搜到的都是c,為有符號整數,這樣會產生負數判斷不正常
  6. $strInfo = @unpack("C2chars", $bin);
  7. $typeCode = intval($strInfo['chars1' ].$strInfo['chars2']);
  8. $fileType = '';
  9. switch( $typeCode )

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

$file_name = '11.doc';echo checkFileType($file_name);?>

複製程式碼

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","gif"),
  49. array("49492A00"" ),
  50. array("424D","bmp"),
  51. array("41433130","dwg"),
  52. array("38425053","psd"),
  53. array(" 7B5C727466","rtf"),
  54. array("3C3F786D6C","xml"),
  55. array("68746D6C3E","html"),
  56. array("68746D6C3E","html"),
  57. array("4656C3E","html"),
  58. array("4656C6"
  59. array("CFAD12FEC5FD746F","dbx"),
  60. array("2142444E","pst"),
  61. array("D0CF11E0","xls/doc"), array("FF575043","wpd"),
  62. array("252150532D41646F6265","epsm/D50457507575075075003750037500037003750037> "pdf" ),
  63. array("E3828596","pwl"),
  64. array("504B0304","zip"),
  65. array("52617221","rar"),
  66. array("52617221","rar"),
  67. array(" 57415645","wav"),
  68. array("41564920","avi"),
  69. array("2E7261FD","ram"),
  70. array("2E524D46,""ram"),
  71. array("2E524D46,""rm")"
  72. array("000001BA","mpg"),
  73. array("000001B3","mpg"),
  74. array("6D6F6F76","mov"),
  75. array("6D6F6F76","mov"),
  76. array("6D6F6F76","mov"),
  77. array(" ,"asf"),
  78. array("4D546864","mid"));
  79. }
  80. public static function getFileType($filename)
  81. {
  82. if(self ::$CheckClass) self::$CheckClass=new self($filename);
  83. $class=self::$CheckClass;
  84. return $class->_getFileType($filename);
  85. }
  86. }
  87. $filename="22.jpg";

  88. echo $filename,"t",cFileTypeCheck::getFileType($filename),"rn";
  89. $filename ="11.doc";
  90. echo $filename,"t",cFileTypeCheck::getFileType($filename),"rn";
  91. //也可以這樣偵測

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


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn