Powerful file type detection function PHP code

Source: Internet
Author: User
  1. /**
  2. * @access Public
  3. * @param string filename
  4. * @param string limit_ext_types allowed file types, with | enclosing types such as: |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) = = ' GIF8 ' && $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) = = ' CWS ' | | 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, 0x200,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. }
Copy Code
  • Related Article

    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.