views:

54

answers:

2

I am writing a script and I need to correctly (I think some mime types can be different from their extensions) get the mime types of files (the files can be of any type).

Web hosting company in use does not have mime_content_type() and is still (don't know which year they will be fixing it) promising to fix the PECL alternative.

Which other way can I go about it (and I don;t have access to shell commands)?

+1  A: 

You could try finfo_file - it returns the mime type.

http://php.net/manual/en/book.fileinfo.php

Mahdi.Montgomery
A: 

Try to use this code :

$mimetypes = array(
    'gif' => 'image/gif',
    'png' => 'image/png',
    'jpg' => 'image/jpg',
    'jpeg' => 'image/jpg',
    'css' => 'text/css',
    'js' => 'text/javascript',
);
$path_parts = pathinfo($file['name']);
if (array_key_exists($path_parts['extension'], $mimetypes)) {
    $mime = $mimetypes[$path_parts['extension']];
} else {
    $mime = 'application/octet-stream';
}
sami boussacsou
That's just guessing the mimetype without actually looking at the contents of the file ... the extension is no guarantee that the file actually contains what it should contain based on extension.
ChrisR
You can also use $_FILES['pic']['type'] .
sami boussacsou