<?php
    
//Example PHP code to determine the type of a file given
    //8 bytes of header data. Much more accurate than checking
    //the user-supplied Content-Type and of course this is far
    //better than relying on checking the file extension :).

    //(C)2004 r1ch.net. I place this code into the public domain
    //in the hope it is useful to somebody.
    
    //open a file
    
$image_data fopen($argv[1], "rb");

    
//grab first 8 bytes, should be enough for most formats
    
$header_bytes fread($image_data8);

    
//close file
    
fclose ($image_data);

    
//compare header to known signatures
    
if (!strncmp ($header_bytes"\xFF\xD8"2))
        
$file_format "JPEG";
    else if (!
strncmp ($header_bytes"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"8))
        
$file_format "PNG";
    else if (!
strncmp ($header_bytes"FWS"3) || !strncmp ($header_bytes"CWS"))
        
$file_format "SWF";
    else if (!
strncmp ($header_bytes"BM"2))
        
$file_format "BMP";
    else if (!
strncmp ($header_bytes"\x50\x4b\x03\x04"4))
        
$file_format "ZIP";
    else if (!
strncmp ($header_bytes"GIF"3))
        
$file_format "GIF";
    else
        
$file_format "unknown";
    
    print 
"The file is a $file_format file.\n";
?>