Hi Friends, MIME (Multipurpose Internet Mail Extensions) as name suggests it is used while sending email attachments mostly. Each file extension have mime types, there are lots of file extensions and its mime types, If you want to see mime types for any new file extension then you can grab it here.
So while making code sometimes we need to find out mime types of different file extension, So instead of making it hard coded I made one function to get mime type php in which I used most used file extension and by passing file name to that function it will return mime type of that file extension.
<?php function get_mime_type($file_name) { // our list of mime types $mime_types = array( "pdf"=>"application/pdf" ,"exe"=>"application/octet-stream" ,"zip"=>"application/zip" ,"docx"=>"application/msword" ,"doc"=>"application/msword" ,"xls"=>"application/vnd.ms-excel" ,"ppt"=>"application/vnd.ms-powerpoint" ,"gif"=>"image/gif" ,"png"=>"image/png" ,"jpeg"=>"image/jpg" ,"jpg"=>"image/jpg" ,"mp3"=>"audio/mpeg" ,"wav"=>"audio/x-wav" ,"mpeg"=>"video/mpeg" ,"mpg"=>"video/mpeg" ,"mpe"=>"video/mpeg" ,"mov"=>"video/quicktime" ,"avi"=>"video/x-msvideo" ,"3gp"=>"video/3gpp" ,"css"=>"text/css" ,"jsc"=>"application/javascript" ,"js"=>"application/javascript" ,"php"=>"text/html" ,"htm"=>"text/html" ,"html"=>"text/html" ); $extension = strtolower(end(explode('.',$file_name))); return $mime_types[$extension]; } ?>
So above is function to get mime type by passing file name to it. Just put this function anywhere in your code from where it available to scope of your code where you are going to use it and then call it like this :
<?php $mime_type = get_mime_type('your_file_name_here.php'); ?>
So by doing this you can easily get mime type of your file extension.Now if you have file extension whose mime type is not included in function then you can add mime type with extension name to function also. You can get mime type for any extension here.
I hope this function will help someone. Let me know in comments if anyone have problem with this function.
I am wordpress Developer, WordPress Developer India and WordPress Freelancer. If you have any projects related to wordpress or PHP you can contact me.