PHP takes the binary file header to determine the file type

Source: Internet
Author: User
  1. /**
  2. * PHP takes the binary file header to quickly determine the file type
  3. *
  4. * @param file $file
  5. * @return String
  6. * @author Lrenwang
  7. * @blog blog.lrenwang.com
  8. */
  9. function Get_extname ($file)
  10. {
  11. $fp = fopen ($file, "RB");
  12. $bin = Fread ($fp, 2); Read-only 2 bytes
  13. Fclose ($FP);
  14. $bin _info = @unpack ("C2chars", $bin);
  15. $code = intval ($bin _info[' chars1 '). $bin _info[' chars2 ']);
  16. Switch ($code) {
  17. Case 7790:
  18. $type = ' exe ';
  19. Break
  20. Case 7784:
  21. $type = ' midi ';
  22. Break
  23. Case 8075:
  24. $type = ' zip ';
  25. Break
  26. Case 8297:
  27. $type = ' rar ';
  28. Break
  29. Case 255216:
  30. $type = ' jpg ';
  31. Break
  32. Case 7173:
  33. $type = ' gif ';
  34. Break
  35. Case 6677:
  36. $type = ' bmp ';
  37. Break
  38. Case 13780:
  39. $type = ' png ';
  40. Break
  41. Default
  42. $type = $code;
  43. Break
  44. }
  45. return $type;
  46. }
  47. ?>
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.