- /* Get the file type by filename *
- * @author chengmo*
- * @version 0.1
- * $filename = "d:/1.png"; Echo Cfiletypecheck::getfiletype ($filename); Printing: PNG
- * Edit bbs.it-home.org
- */
- Class Cfiletypecheck
- {
- private static $_typelist=array ();
- private static $CheckClass =null;
- Private function __construct ($filename)
- {
- self::$_typelist= $this->gettypelist ();
- }
- /**
- * Process file Type mapping Relationship table *
- *
- * @param string $filename file type
- * @return string file type, not found returned: other
- */
- Private Function _getfiletype ($filename)
- {
- $filetype = "other";
- if (!file_exists ($filename)) throw new Exception ("No found file!");
- $file = @fopen ($filename, "RB");
- if (! $file) throw new Exception ("File refuse!");
- $bin = Fread ($file, 15); Read-only 15 bytes each different file type, header information is not the same.
- Fclose ($file);
- $typelist =self::$_typelist;
- foreach ($typelist as $v)
- {
- $blen =strlen (Pack ("h*", $v [0]); Gets the number of file header tokens in bytes
- $tbin =substr ($bin, 0,intval ($blen)); Need to compare file header lengths
- if (Strtolower ($v [0]) ==strtolower (Array_shift (Unpack ("h*", $tbin))))
- {
- return $v [1];
- }
- }
- return $filetype;
- }
- /**
- * Get file header and File type mapping table *
- *
- * @return Array Array (array (key,value) ...)
- */
- Public Function Gettypelist ()
- {
- return Array (Array ("Ffd8ffe1", "jpg"),
- Array ("89504E47", "PNG"),
- Array ("47494638", "gif"),
- Array ("49492a00", "TIF"),
- Array ("424D", "BMP"),
- Array ("41433130", "DWG"),
- Array ("38425053", "PSD"),
- Array ("7b5c727466", "RTF"),
- Array ("3C3F786D6C", "xml"),
- Array ("68746d6c3e", "html"),
- Array ("44656c69766572792d646174", "eml"),
- Array ("cfad12fec5fd746f", "dbx"),
- Array ("2142444E", "PST"),
- Array ("D0cf11e0", "Xls/doc"),
- Array ("5374616e64617264204a", "MDB"),
- Array ("FF575043", "WPD"),
- Array ("252150532d41646f6265", "Eps/ps"),
- Array ("255044462d312e", "PDF"),
- Array ("E3828596", "PWL"),
- Array ("504b0304", "zip"),
- Array ("52617221", "rar"),
- Array ("57415645", "wav"),
- Array ("41564920", "avi"),
- Array ("2E7261FD", "Ram"),
- Array ("2e524d46", "rm"),
- Array ("000001BA", "mpg"),
- Array ("000001b3", "mpg"),
- Array ("6d6f6f76", "mov"),
- Array ("3026b2758e66cf11", "ASF"),
- Array ("4d546864", "mid"));
- }
- public static function Getfiletype ($filename)
- {
- if (!self:: $CheckClass) Self:: $CheckClass =new self ($filename);
- $class =self:: $CheckClass;
- Return $class->_getfiletype ($filename);
- }
- }
- ?>
Copy Code |