Php code for file type detection based on file header information

Source: Internet
Author: User
Php code for file type detection based on file header information

  1. // Check the file type

  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, and all found on the Internet are c, which is a signed integer, resulting in negative numbers and incorrect judgment.
  7. $ StrInfo = @ unpack ("C2chars", $ bin );
  8. $ TypeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']);
  9. $ FileType = '';

  10. Switch ($ typeCode)

  11. {
  12. Case '20140901 ':
  13. Return $ typeCode. ':'. 'jpg ';
  14. Break;
  15. Case '20140901 ':
  16. Return $ typeCode. ':'. 'GIF ';
  17. Break;
  18. Case '20140901 ':
  19. Return $ typeCode. ':'. 'PNG ';
  20. Break;
  21. Case '20140901 ':
  22. Return $ typeCode. ':'. 'bmp ';
  23. Break;
  24. Case '20140901 ':
  25. Return $ typeCode. ':'. 'exe ';
  26. Break;
  27. Case '20140901 ':
  28. Return $ typeCode. ':'. 'midi ';
  29. Break;
  30. Case '20140901 ':
  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. verify the class of the file type based on the php file header information

  1. /* Obtain the file type by file name *

  2. * $ Filename = "d:/1.png"; echo cFileTypeCheck: getFileType ($ filename); print: 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 the file type ing table *
  14. *
  15. * @ Param string $ filename File type
  16. * @ Return string file type. the returned value is 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 for different file types and different header information.
  25. Fclose ($ file );
  26. $ Typelist = self ::$ _ TypeList;
  27. Foreach ($ typelist as $ v)
  28. {
  29. $ Blen = strlen (pack ("H *", $ v [0]); // get the number of bytes marked by the file header
  30. $ Tbin = substr ($ bin, 0, intval ($ blen); // compare the file header length
  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 the file header and file type ing table *
  40. *
  41. * @ Return array ('key', value )...)
  42. */
  43. Public function getTypeList ()
  44. {
  45. Return array ("FFD8FFE1", "jpg "),
  46. Array ("89504E47", "png "),
  47. Array ("47494638", "gif "),
  48. Array ("49347a00", "tif "),
  49. Array ("mongod", "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 ("5374616e64617213204a", "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 ("2e00001fd", "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. // This can also be detected

  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) = 'gif 8' & $ 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) = 'done' | 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, 0 x, 2) = "/x09/x08" | $ extname = 'XLS ')
  133. {
  134. $ Format = 'XLS ';
  135. } Elseif (substr ($ str, 0 x, 4) = "/xFD/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. ?>

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.