Posted By

o0110o on 10/20/12


Tagged

curl function phpmime-types


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

o0110o


A function to get file mime-types with curl.


 / Published in: PHP
 

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.

  1. function file_mime($_path) {
  2. $ch = curl_init($_path);
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  4. curl_exec($ch);
  5. $content_info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  6. curl_close($ch);
  7. $content_parts = explode(";", $content_info);
  8. $mime = $content_parts[0];
  9. if(isset($mime) && !empty($mime)){return $mime;}
  10. else if (function_exists('finfo_open')) {
  11. $get_info = new finfo;
  12. $mime = $finfo->file($_path, FILEINFO_MIME);
  13. return $mime;
  14. }
  15. else { $mime = 'application/octet-stream'; return $mime;}
  16. }

Report this snippet  

You need to login to post a comment.