[PHP] 파일 포맷 알아내기
- 08-23
- 45,369 회
- 0 건
[code]
<?php
function get_filetype($filename)
{
$types = [
'jpg' => [0, "\xFF\xD8\xFF"],
'png' => [0, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"],
'bmp' => [0, "\x42\x4D"],
'gif' => [0, "\x47\x49\x46\x38"],
'mp4' => [4, "\x66\x74\x79\x70\x69\x73\x6F\x6D"],
'webp' => [8, "\x57\x45\x42\x50"]
];
$rst = false;
if(is_file($filename) && $hnd = fopen($filename, 'rb'))
{
$buf = fread($hnd, 16);
foreach($types as $type => $patt)
{
if(strcmp($patt[1], substr($buf, $patt[0], strlen($patt[1]))) == 0)
{
$rst = $type;
break;
}
}
fclose($hnd);
}
return $rst;
}
[/code]