A function to get file mime-types with curl.


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.