Detecting file types
- function Checkfiletype ($fileName) {
- $file = fopen ($fileName, "RB");
- $bin = Fread ($file, 2); Read-only 2 bytes
- Fclose ($file);
- c is an unsigned integer, the net search is C, signed integer, this will produce negative judgment is not normal
- $strInfo = @unpack ("C2chars", $bin);
- $typeCode = Intval ($strInfo [' chars1 ']. $strInfo [' chars2 ']);
- $fileType = ";
Switch ($typeCode)
- {
- Case ' 255216 ':
- Return $typeCode. ' : ' .' JPG ';
- Break
- Case ' 7173 ':
- Return $typeCode. ' : ' .' GIF ';
- Break
- Case ' 13780 ':
- Return $typeCode. ' : ' .' PNG ';
- Break
- Case ' 6677 ':
- Return $typeCode. ' : ' .' BMP ';
- Break
- Case ' 7790 ':
- Return $typeCode. ' : ' .' EXE ';
- Break
- Case ' 7784 ':
- Return $typeCode. ' : ' .' Midi ';
- Break
- Case ' 8297 ':
- Return $typeCode. ' : ' .' RAR ';
- Break
- Default
- Return $typeCode. ' : ' .' Unknown ';
- Break
- }
- return $typeCode;
- }
$file _name = ' 11.doc ';
- Echo Checkfiletype ($file _name);
- ?>
Copy Code2, according to the PHP file header information to verify the file type of the class
/* Get the file type by filename *
- * $filename = "d:/1.png"; Echo Cfiletypecheck::getfiletype ($filename); Printing: PNG
- */
- 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 (' 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);
- }
- }
$filename = "22.jpg";
- echo $filename, "T", Cfiletypecheck::getfiletype ($filename), "RN";
- $filename = "11.doc";
- echo $filename, "T", Cfiletypecheck::getfiletype ($filename), "RN";
You can also detect this
- $filename = ' 22.jpg ';
- $extname = Strtolower (substr ($filename, Strrpos ($filename, '. ') + 1));
- echo $extname. '
';
- $file = @fopen ($filename, ' RB ');
- if ($file)
- {
- $str = @fread ($file, 0x400); Read the first 1024 bytes
- Echo substr ($str, 0, 4);
- @fclose ($file);
- }
- if (substr ($str, 0, 4) = = ' MThd ' && $extname! = ' txt ')
- {
- $format = ' mid ';
- }
- ElseIf (substr ($str, 0, 4) = = ' RIFF ' && $extname = = ' wav ')
- {
- $format = ' wav ';
- }
- ElseIf (substr ($str, 0, 3) = = "/xff/xd8/xff")
- {
- $format = ' jpg ';
- }
- ElseIf (substr ($str, 0, 4) = = ' GIF8 ' && $extname! = ' txt ')
- {
- $format = ' gif ';
- }
- ElseIf (substr ($str, 0, 8) = = "/x89/x50/x4e/x47/x0d/x0a/x1a/x0a")
- {
- $format = ' png ';
- }
- ElseIf (substr ($str, 0, 2) = = ' BM ' && $extname! = ' txt ')
- {
- $format = ' bmp ';
- }
- ElseIf ((substr ($str, 0, 3) = = ' CWS ' | | substr ($STR, 0, 3) = = ' FWS ') && $extname! = ' txt ')
- {
- $format = ' swf ';
- }
- ElseIf (substr ($str, 0, 4) = = "/xd0/xcf/x11/xe0")
- {//d0cf11e = = Docfile = = Microsoft Office Document
- if (substr ($str, 0x200,4) = = "/xec/xa5/xc1/x00" | | $extname = = ' Doc ')
- {
- $format = ' Doc ';
- }
- ElseIf (substr ($str, 0x200,2) = = "/x09/x08" | | $extname = = ' xls ')
- {
- $format = ' xls ';
- } elseif (substr ($str, 0x200,4) = = "/xfd/xff/xff/xff" | | $extname = = ' ppt ')
- {
- $format = ' ppt ';
- }
- } elseif (substr ($str, 0, 4) = = "pk/x03/x04")
- {
- $format = ' zip ';
- } elseif (substr ($str, 0, 4) = = ' rar! ' && $extname! = ' txt ')
- {
- $format = ' rar ';
- } elseif (substr ($str, 0, 4) = = "/x25pdf")
- {
- $format = ' pdf ';
- } elseif (substr ($str, 0, 3) = = "/x30/x82/x0a")
- {
- $format = ' cert ';
- } elseif (substr ($str, 0, 4) = = ' ITSF ' && $extname! = ' txt ')
- {
- $format = ' chm ';
- } elseif (substr ($str, 0, 4) = = "/X2ERMF")
- {
- $format = ' RM ';
- } elseif ($extname = = ' sql ')
- {
- $format = ' sql ';
- } elseif ($extname = = ' txt ')
- {
- $format = ' txt ';
- }
- Echo $format;
- ?>
Copy Code |