HVIS din vært stadig bruger php 5.2 og ikke har adgang til filinfo-funktionerne, kan du teste filens header-signatur (magiske tal) for at bestemme mime-typen
function mimetype($data)
{
//File signatures with their associated mime type
$Types = array(
"474946383761"=>"image/gif", //GIF87a type gif
"474946383961"=>"image/gif", //GIF89a type gif
"89504E470D0A1A0A"=>"image/png",
"FFD8FFE0"=>"image/jpeg", //JFIF jpeg
"FFD8FFE1"=>"image/jpeg", //EXIF jpeg
"FFD8FFE8"=>"image/jpeg", //SPIFF jpeg
"25504446"=>"application/pdf",
"377ABCAF271C"=>"application/zip", //7-Zip zip file
"504B0304"=>"application/zip", //PK Zip file ( could also match other file types like docx, jar, etc )
);
$Signature = substr($data,0,60); //get first 60 bytes shouldnt need more then that to determine signature
$Signature = array_shift(unpack("H*",$Signature)); //String representation of the hex values
foreach($Types as $MagicNumber => $Mime)
{
if( stripos($Signature,$MagicNumber) === 0 )
return $Mime;
}
//Return octet-stream (binary content type) if no signature is found
return "application/octet-stream";
}
BEMÆRK: Nogle signaturer kan matche partialer af andre, f.eks. matcher PK Zip-filsignaturen de første 4 bytes af java archive (.jar) filsignatur, ekstra sætninger ville være nødvendige i foreach loop for at bestemme den korrekte signatur for mime type , men for din situation burde dette gøre det.
En opdateret liste over filsignaturer kan findes på http://www.garykessler.net/library /file_sigs.html hvis nogen har brug for flere filsignaturtyper.