Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the relation between Mime Type and file extension

my question is: Is there some relation between a file extension and it's mime type? I mean, if i get a file, for instance, .php and change it's extension to .png will also change it's mime type?

like image 388
Arthur Mastropietro Avatar asked Feb 10 '23 16:02

Arthur Mastropietro


1 Answers

Short answer: Yes.

Slightly longer answer: Mime types and file extensions provide hints to how to deal with a file. Whereas file extensions are commonly used for your OS to decide what program to open a file with, Mime types are used by your browser to decide how to present some data (or the server on how to interpret received data). Both are optional but it's a good practice to have an agreement. Changing the mime type a file is served as depends on your webserver. I believe Apache has settings somewhere to map from extensions to mime types. If you have your own back end serving content you can potentially serve content with any arbitrary mime type, for example, in PHP:

<?php
// We'll be outputting a PDF
header('Content-Type: application/pdf');
...

or

<?php
header('Content-Type: application/javascript');
echo "//script code here"
like image 114
Josep Valls Avatar answered Feb 12 '23 15:02

Josep Valls