Return to Snippet

Revision: 60096
at October 20, 2012 09:45 by o0110o


Initial Code
function file_mime($_path) {
  $ch = curl_init($_path);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_exec($ch);
  $content_info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  curl_close($ch);
  $content_parts = explode(";", $content_info);
  $mime = $content_parts[0];
  if(isset($mime) && !empty($mime)){return $mime;}
  else if (function_exists('finfo_open')) {
    $get_info = new finfo;
    $mime = $finfo->file($_path, FILEINFO_MIME);
    return $mime;
  }
  else { $mime = 'application/octet-stream'; return $mime;}
}

Initial URL

                                

Initial Description
This is a function to retrieve the mime-type of a file by checking the header response with curl. It accepts the file-path as a parameter & it also has a fallback for empty returns.

Initial Title
A function to get file mime-types with curl.

Initial Tags
curl, function

Initial Language
PHP