// Check the file type
- Function checkFileType ($ fileName ){
- $ File = fopen ($ fileName, "rb ");
- $ Bin = fread ($ file, 2); // read-only 2 bytes
- Fclose ($ file );
- // C is an unsigned integer, and all found on the Internet are c, which is a signed integer, resulting in negative numbers and incorrect judgment.
- $ StrInfo = @ unpack ("C2chars", $ bin );
- $ TypeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']);
- $ FileType = '';
Switch ($ typeCode)
- {
- Case '20140901 ':
- Return $ typeCode. ':'. 'jpg ';
- Break;
- Case '20140901 ':
- Return $ typeCode. ':'. 'GIF ';
- Break;
- Case '20140901 ':
- Return $ typeCode. ':'. 'PNG ';
- Break;
- Case '20140901 ':
- Return $ typeCode. ':'. 'bmp ';
- Break;
- Case '20140901 ':
- Return $ typeCode. ':'. 'exe ';
- Break;
- Case '20140901 ':
- Return $ typeCode. ':'. 'midi ';
- Break;
- Case '20140901 ':
- Return $ typeCode. ':'. 'rar ';
- Break;
- Default:
- Return $ typeCode. ':'. 'unknown ';
- Break;
- }
- // Return $ typeCode;
- }
$ File_name = '11.doc ';
- Echo checkFileType ($ file_name );
- ?>
2. verify the class of the file type based on the php file header information
/* Obtain the file type by file name *
- * $ Filename = "d:/1.png"; echo cFileTypeCheck: getFileType ($ filename); print: png
- */
- Class cFileTypeCheck
- {
- Private static $ _ TypeList = array ();
- Private static $ CheckClass = null;
- Private function _ construct ($ filename)
- {
- Self: $ _ TypeList = $ this-> getTypeList ();
- }
-
- /**
- * Process the file type ing table *
- *
- * @ Param string $ filename File type
- * @ Return string file type. the returned value is 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 for different file types and different header information.
- Fclose ($ file );
-
- $ Typelist = self ::$ _ TypeList;
- Foreach ($ typelist as $ v)
- {
- $ Blen = strlen (pack ("H *", $ v [0]); // get the number of bytes marked by the file header
- $ Tbin = substr ($ bin, 0, intval ($ blen); // compare the file header length
-
- If (strtolower ($ v [0]) = strtolower (array_shift (unpack ("H *", $ tbin ))))
- {
- Return $ v [1];
- }
- }
- Return $ filetype;
- }
-
- /**
- * Get the file header and file type ing table *
- *
- * @ Return array ('key', value )...)
- */
- Public function getTypeList ()
- {
- Return array ("FFD8FFE1", "jpg "),
- Array ("89504E47", "png "),
- Array ("47494638", "gif "),
- Array ("49347a00", "tif "),
- Array ("mongod", "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 ("5374616e64617213204a", "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 ("2e00001fd", "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 ";
// This can also be detected
- $ 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) = 'gif 8' & $ 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) = 'done' | 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, 0 x, 2) = "/x09/x08" | $ extname = 'XLS ')
- {
- $ Format = 'XLS ';
- } Elseif (substr ($ str, 0 x, 4) = "/xFD/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;
- ?>
|