PHP Question: Getting File Mime Type

See all posts Reply

PHP Question: Getting File Mime Type new!
by Zach, 17 years, 1 month ago
Hey

I know that this isn't related to the script, but if it is possible to help me out that would be great. I am trying to get the MIME type of a file already on the server. What would be the best way to do this?

Thanks for any help you can provide.Reply
Re: PHP Question: Getting File Mime Type new!
by colin, 17 years, 1 month ago
Look at the code of the class. There are several methods. One -the last one in the code below- uses the compilation MIME magic, now replaced by the fileinfo extension .
 // we try to retrieve the MIME type
$info = getimagesize($this->file_src_pathname);
$this->file_src_mime = (array_key_exists('mime', $info) ? $info['mime'] : NULL);
// if we don't have a MIME type, we attempt to retrieve it the old way
if (empty($this->file_src_mime)) {
  $mime = (array_key_exists(2, $info) ? $info[2] : NULL); // 1 = GIF, 2 = JPG, 3 = PNG
  $this->file_src_mime = ($mime==1 ? 'image/gif' : ($mime==2 ? 'image/jpeg' : ($mime==3 ? 'image/png' : NULL)));
}
// if we still don't have a MIME type, we attempt to retrieve it otherwise
  if (empty($this->file_src_mime) && function_exists('mime_content_type')) {
  $this->file_src_mime = mime_content_type($this->file_src_pathname);
} 
Reply