PHP code to read the file header to determine the type of file, support pictures, RAR, exe and other suffixes.
Case:
Copy Code code as follows:
<?php $filename = "11.jpg";
The path to the picture can be d:/upload/11.jpg and so on absolute path
$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 255216: $fileType = ' jpg '; Break
Case 7173: $fileType = ' gif '; Break
Case 6677: $fileType = ' bmp '; Break
Case 13780: $fileType = ' png '; Break
Default:echo ' unknown ';
}
Echo ' This is a '. $fileType. ' File: '. $typeCode;
Case:
Copy Code code as follows:
?>
Linux PHP also has a function to determine the file type
<?php
echo mime_content_type (' 11.gif '). "\ n";
echo mime_content_type (' 22.php ');
?>