This article mainly introduces the PHP implementation through the file header to determine the format of the method, involving PHP file reading and string parsing related skills, the need for friends can refer to the next
Specific as follows:
function Judgefile ($file, $form) {if (!empty ($file) &&!empty ($form)) {$filehead = fopen ($file, ' R '); $bin = Fread ( $filehead, 2); fclose ($filehead); $data = Unpack (' C2chars ', $bin); $type _code = Intval ($data [' chars1 ']. $data [' chars2 ']) Switch ($type _code) {case 7790: $fileType = ' exe '; Case 7784: $fileType = ' midi '; Case 8075: $fileType = ' zip '; Case 8297: $fileType = ' rar '; Case 255216: $fileType = ' jpg '; Case 7173: $fileType = ' gif '; Case 6677: $fileType = ' bmp '; Case 13780: $fileType = ' png '; Default: $fileType = ' unknown '; if (!is_array ($form)) {if ($fileType = = $form) {return true;} Else{return false;}} Else{if (In_array ($file, $form)) {return true;} Else{return FALSE;}}} Else{return false;}}
Summary: The above is the entire content of this article, I hope to be able to help you learn.