<?php // 判断文件真实类型 function get_file_type($fileName) { $file = fopen($fileName, "rb"); $bin = fread($file, 2); // 只读2字节 fclose($file); $strInfo = @unpack("C2chars", $bin); // 将字节转换为字节二进制的数组 $typeCode = (int)($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' && $strInfo['chars2'] == '-40') return 'jpg'; if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') return 'png'; return $fileType; }
标签:文件,真实,后缀,修改,防止,php From: https://www.cnblogs.com/mldyfre/p/18321176