[RESOLVED] File Content Type
is there an easy way to get the content type of a file?
I am working on a file sharing script and i need to get the file content type for download.
the only way i can think of is like having a database with all the different content types and getting it by the file extension...
Re: [RESOLVED] File Content Type
Dylan, the first four bytes of the file you upload can also be used to verify the file type. A JPEG file for example should always start with "ff d8 ff e0", if it doesn't then you can be absolutely sure that the file is not a JPEG. That doesn't prevent other data from being hidden within it but is is another step you can use:
PHP Code:
// example
$jpeg = chr(0xff) . chr(0xd8) . chr(0xff) . chr(0xe0);
$fhwnd = fopen('file.jpg', 'rb');
$signature = fread($fhwnd, 4);
if ($signature != $jpeg) {
// wrong file type
}
You can see some other common signatures here: http://www.garykessler.net/library/file_sigs.html. Watch out though, some file types may have several signatures.
Re: [RESOLVED] File Content Type
thanks for that vis. i will look into it!