Home >Backend Development >PHP Tutorial >PHP implements a method to determine the format through the file header
This article mainly introduces the method of PHP to determine the format through the file header, involving related skills of PHP file reading and string parsing. Friends in need can refer to it
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; } }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
The reasons and solutions for the discontinuity of ID settings in PHP after they are incremented
PHP implementation judgment Method for mobile devices
PHP strip_tags() Strips the HTML tags in the string
The above is the detailed content of PHP implements a method to determine the format through the file header. For more information, please follow other related articles on the PHP Chinese website!