Php uses the file header to determine the format,
This article describes how php uses the file header to determine the format. We will share this with you for your reference. The details are 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';break; case 7784: $fileType = 'midi';break; case 8075: $fileType = 'zip';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: $fileType = 'unknown';break;}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;}}
Supplement: The editor recommends a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:
Php code online formatting and beautification tools:
Http://tools.jb51.net/code/phpformat
In addition, because php belongs to the C language style, the following tool can also format php code:
C language style/HTML/CSS/json code formatting and beautification tools:
Http://tools.jb51.net/code/ccode_html_css_json