PHP code to detect file types based on file header information

Source: Internet
Author: User
Tags fread unpack
  1. Detecting file types

  2. function Checkfiletype ($fileName) {
  3. $file = fopen ($fileName, "RB");
  4. $bin = Fread ($file, 2); Read-only 2 bytes
  5. Fclose ($file);
  6. c is an unsigned integer, the net search is C, signed integer, this will produce negative judgment is not normal
  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. ?>

Copy Code

2, according to the PHP file header information to verify the file type of the class

  1. /* Get the file type by filename *

  2. * $filename = "d:/1.png"; Echo Cfiletypecheck::getfiletype ($filename); Printing: 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. * Process file Type mapping Relationship table *
  14. *
  15. * @param string $filename file type
  16. * @return string file type, not found returned: 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); Read-only 15 bytes each different file type, header information is not the same.
  25. Fclose ($file);
  26. $typelist =self::$_typelist;
  27. foreach ($typelist as $v)
  28. {
  29. $blen =strlen (Pack ("h*", $v [0]); Gets the number of file header tokens in bytes
  30. $tbin =substr ($bin, 0,intval ($blen)); Need to compare file header lengths
  31. if (Strtolower ($v [0]) ==strtolower (Array_shift (Unpack ("h*", $tbin))))
  32. {
  33. return $v [1];
  34. }
  35. }
  36. return $filetype;
  37. }
  38. /**
  39. * Get file header and File type mapping table *
  40. *
  41. * @return 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. You can also detect this

  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); Read the first 1024 bytes
  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. ?>

Copy Code
  • 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.