An example of php code used to determine the file upload type
- /**
- * Determine the type of the uploaded file
- * Edit bbs.it-home.org
- */
- Function file_type ($ filename)
- {
- $ File = fopen ($ filename, "rb ");
- $ Bin = fread ($ file, 2); // read-only 2 bytes
- Fclose ($ file );
- $ StrInfo = @ unpack ("C2chars", $ bin );
- $ TypeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']);
- $ FileType = '';
- Switch ($ typeCode)
- {
- Case 7790:
- $ FileType = 'exe ';
- Break;
- Case 7784:
- $ FileType = 'midi ';
- Break;
- Case 8297:
- $ FileType = 'rar ';
- Break;
- Case 8075:
- $ FileType = 'Zip ';
- Break;
- Case 255216:
- $ FileType = 'jpg ';
- Break;
- Case 7173:
- $ FileType = 'GIF ';
- Break;
- Case 6677:
- $ FileType = 'bmp ';
- Break;
- Case 13780:
- $ FileType = 'PNG ';
- Break;
- Default:
- $ FileType = 'unknown: '. $ typeCode;
- }
- // Fix
- If ($ strInfo ['chars1'] = '-1' AND $ strInfo ['chars2'] ='-40') return 'jpg ';
- If ($ strInfo ['chars1'] = '-119' AND $ strInfo ['chars2'] = '80') return 'PNG ';
- Return $ fileType;
- }
- // Call
- Echo file_type ('Start. php'); // 6063 or 6033
- ?>
I don't know if the definition of 6063 or 6033 in turn refers to php is not rigorous enough.
The above code does not make a good decision on the file type for constructing fake images. In this case, you can consider using getimagesize to determine. the reference code is as follows:
- /**
- * Getimagesize determines the file type
- * Edit bbs.it-home.org
- */
- If (in_array ($ attach ['ext '], array ('jpg', 'jpeg ', 'GIF', 'PNG', 'swf ', 'bmp ')) & function_exists ('getimagesize ')&&! @ Getimagesize ($ target ))
- {
- Unlink ($ target );
- Upload_error ('post _ attachment_ext_notallowed ', $ attacharray );
- }
- ?>
|