php文件上传检测文件头
function check_file_type($file, $allow_type = array('gif', 'jpg', 'jpeg', 'png', 'bmp'))
{
$file = fopen($file, 'rb');
$bin = fread($file, 2); //只读2字节
fclose($file);
$strInfo = @unpack('C2chars', $bin);
$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
$fileType = '';
switch ($typeCode)
{
case 255216:
$fileType = 'jpg';
break;
case 7173:
$fileType = 'gif';
break;
case 6677:
$fileType = 'bmp';
break;
case 13780:
$fileType = 'png';
break;
default:
$fileType = 'unknown';
}
if (!in_array($fileType, $allow_type))
{
return false;
}
return true;
}
PHP函数实现文件上传类型检测:检查文件头
该篇文章介绍了如何使用PHP函数检查上传文件的类型,通过读取文件头的两个字节并解析来确定是GIF、JPEG、PNG还是BMP,确保上传的文件符合预设类型。
6698

被折叠的 条评论
为什么被折叠?



