Powerful file type detection function php code

Source: Internet
Author: User
Powerful file type detection function php code

  1. /**
  2. * @ Access public
  3. * @ Param string filename file name
  4. * @ Param string limit_ext_types: the file type allowed. | used | the surrounded type is: | gif | txt |
  5. * @ Return string
  6. * By http://bbs.it-home.org
  7. */
  8. Function check_file_type ($ filename, $ limit_ext_types = ''){
  9. $ Extname = strtolower (substr ($ filename, strrpos ($ filename, '.') + 1 ));
  10. If ($ limit_ext_types &&
  11. Stristr ($ limit_ext_types, '|'. $ extname. '|') = false ){
  12. Return '';
  13. }
  14. $ Str = $ format = '';
  15. $ File = @ fopen ($ filename, 'RB ');
  16. If ($ file ){
  17. $ Str = @ fread ($ file, 0x400); // read the first 1024 bytes
  18. @ Fclose ($ file );
  19. }
  20. Else {
  21. $ Format = $ extname;
  22. }
  23. If ($ format = ''& strlen ($ str)> = 2 ){
  24. If (substr ($ str, 0, 4) = 'mthd' & $ extname! = 'Txt '){
  25. $ Format = 'mid ';
  26. }
  27. Elseif (substr ($ str, 0, 4) = 'riff' & $ extname = 'wav '){
  28. $ Format = 'wav ';
  29. }
  30. Elseif (substr ($ str, 0, 3) = "xFFxD8xFF "){
  31. $ Format = 'jpg ';
  32. }
  33. Elseif (substr ($ str, 0, 4) = 'gif 8' & $ extname! = 'Txt '){
  34. $ Format = 'GIF ';
  35. }
  36. Elseif (substr ($ str, 0, 8) = "x89x50x4Ex47x0Dx0Ax1Ax0A "){
  37. $ Format = 'PNG ';
  38. }
  39. Elseif (substr ($ str, 0, 2) = 'bm '& $ extname! = 'Txt '){
  40. $ Format = 'bmp ';
  41. }
  42. Elseif (substr ($ str, 0, 3) = 'done' | substr ($ str, 0, 3) = 'fws ')
  43. & $ Extname! = 'Txt '){
  44. $ Format = 'swf ';
  45. }
  46. Elseif (substr ($ str, 0, 4) = "xD0xCFx11xE0") {// D0CF11E = DOCFILE = Microsoft Office Document
  47. If (substr ($ str, 0x200,4) = "xECxA5xC1x00"
  48. | $ Extname = 'doc '){
  49. $ Format = 'doc ';
  50. }
  51. Elseif (substr ($ str, 0 x, 2) = "x09x08" | $ extname = 'XLS '){
  52. $ Format = 'XLS ';
  53. }
  54. Elseif (substr ($ str, 0x200,4) = "xFDxFFxFFxFF"
  55. | $ Extname = 'ppt '){
  56. $ Format = 'ppt ';
  57. }
  58. }
  59. Elseif (substr ($ str, 0, 4) = "PKx03x04 "){
  60. $ Format = 'Zip ';
  61. }
  62. Elseif (substr ($ str, 0, 4) = 'rar! '& $ Extname! = 'Txt '){
  63. $ Format = 'rar ';
  64. }
  65. Elseif (substr ($ str, 0, 4) = "x25PDF "){
  66. $ Format = 'PDF ';
  67. }
  68. Elseif (substr ($ str, 0, 3) = "x30x82x0A "){
  69. $ Format = 'cert ';
  70. }
  71. Elseif (substr ($ str, 0, 4) = 'itsf '& $ extname! = 'Txt '){
  72. $ Format = 'chm ';
  73. }
  74. Elseif (substr ($ str, 0, 4) = "x2ERMF "){
  75. $ Format = 'rm ';
  76. }
  77. Elseif ($ extname = 'SQL '){
  78. $ Format = 'SQL ';
  79. }
  80. Elseif ($ extname = 'txt '){
  81. $ Format = 'txt ';
  82. }
  83. }
  84. If ($ limit_ext_types &&
  85. Stristr ($ limit_ext_types, '|'. $ format. '|') = false ){
  86. $ Format = '';
  87. }
  88. Return $ format;
  89. }

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.