PHP custom class for detecting file types via file header

Source: Internet
Author: User
  1. /* Get the file type by filename *
  2. * @author chengmo*
  3. * @version 0.1
  4. * $filename = "d:/1.png"; Echo Cfiletypecheck::getfiletype ($filename); Printing: PNG
  5. * Edit bbs.it-home.org
  6. */
  7. Class Cfiletypecheck
  8. {
  9. private static $_typelist=array ();
  10. private static $CheckClass =null;
  11. Private function __construct ($filename)
  12. {
  13. self::$_typelist= $this->gettypelist ();
  14. }
  15. /**
  16. * Process file Type mapping Relationship table *
  17. *
  18. * @param string $filename file type
  19. * @return string file type, not found returned: other
  20. */
  21. Private Function _getfiletype ($filename)
  22. {
  23. $filetype = "other";
  24. if (!file_exists ($filename)) throw new Exception ("No found file!");
  25. $file = @fopen ($filename, "RB");
  26. if (! $file) throw new Exception ("File refuse!");
  27. $bin = Fread ($file, 15); Read-only 15 bytes each different file type, header information is not the same.
  28. Fclose ($file);
  29. $typelist =self::$_typelist;
  30. foreach ($typelist as $v)
  31. {
  32. $blen =strlen (Pack ("h*", $v [0]); Gets the number of file header tokens in bytes
  33. $tbin =substr ($bin, 0,intval ($blen)); Need to compare file header lengths
  34. if (Strtolower ($v [0]) ==strtolower (Array_shift (Unpack ("h*", $tbin))))
  35. {
  36. return $v [1];
  37. }
  38. }
  39. return $filetype;
  40. }
  41. /**
  42. * Get file header and File type mapping table *
  43. *
  44. * @return Array Array (array (key,value) ...)
  45. */
  46. Public Function Gettypelist ()
  47. {
  48. return Array (Array ("Ffd8ffe1", "jpg"),
  49. Array ("89504E47", "PNG"),
  50. Array ("47494638", "gif"),
  51. Array ("49492a00", "TIF"),
  52. Array ("424D", "BMP"),
  53. Array ("41433130", "DWG"),
  54. Array ("38425053", "PSD"),
  55. Array ("7b5c727466", "RTF"),
  56. Array ("3C3F786D6C", "xml"),
  57. Array ("68746d6c3e", "html"),
  58. Array ("44656c69766572792d646174", "eml"),
  59. Array ("cfad12fec5fd746f", "dbx"),
  60. Array ("2142444E", "PST"),
  61. Array ("D0cf11e0", "Xls/doc"),
  62. Array ("5374616e64617264204a", "MDB"),
  63. Array ("FF575043", "WPD"),
  64. Array ("252150532d41646f6265", "Eps/ps"),
  65. Array ("255044462d312e", "PDF"),
  66. Array ("E3828596", "PWL"),
  67. Array ("504b0304", "zip"),
  68. Array ("52617221", "rar"),
  69. Array ("57415645", "wav"),
  70. Array ("41564920", "avi"),
  71. Array ("2E7261FD", "Ram"),
  72. Array ("2e524d46", "rm"),
  73. Array ("000001BA", "mpg"),
  74. Array ("000001b3", "mpg"),
  75. Array ("6d6f6f76", "mov"),
  76. Array ("3026b2758e66cf11", "ASF"),
  77. Array ("4d546864", "mid"));
  78. }
  79. public static function Getfiletype ($filename)
  80. {
  81. if (!self:: $CheckClass) Self:: $CheckClass =new self ($filename);
  82. $class =self:: $CheckClass;
  83. Return $class->_getfiletype ($filename);
  84. }
  85. }
  86. ?>
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.