php判断上传文件类型(防止篡改后缀)

2022-02-25发布 3749阅读
PHP
<?php

$file = file_get_contents("11.txt");
$bin = substr($file,0,2);
$strInfo = @unpack("C2chars", $bin);
$typeCode = intval($strInfo[&#039;chars1&#039;].$strInfo[&#039;chars2&#039;]);
$fileType = &#039;&#039;;
switch ($typeCode)
{
    case 7790:
        $fileType = &#039;exe&#039;;
        break;
    case 7784:
        $fileType = &#039;midi&#039;;
        break;
    case 8297:
        $fileType = &#039;rar&#039;;
        break;
    case 255216:
        $fileType = &#039;jpg&#039;;
        break;
    case 7173:
        $fileType = &#039;gif&#039;;
        break;
    case 6677:
        $fileType = &#039;bmp&#039;;
        break;
    case 13780:
        $fileType = &#039;png&#039;;
        break;
    default:
        echo &#039;unknown&#039;;
}
echo &#039;<br/>this is a(an) &#039;.$fileType.&#039; file:&#039;.$typeCode;
?>