一般我们都是按照文件扩展名来判断文件类型但是这个很不靠谱轻易就通过修改扩展名来躲避了一般必须要读取文件信息来识别
<?php
$files = array(/testjpg testpng)
$fileTypes = array(
=> exe
=> midi
=> zip
=> rar
=> jpg
=> gif
=> bmp
=> png
)
foreach($files as $file) {
$fp = fopen($file rb)
$bin = fread($fp ) // 只读头两个字节
fclose($fp)
$strInfo = @unpack(Cchars $bin)
$typeCode = intval($strInfo[chars]$strInfo[chars])
$fileType = isset($fileTypes[$typeCode]) ? $fileTypes[$typeCode] : unknown;
echo $file type : <b> $fileType </b> code : <b> $fileType </b><br />;
}