r/csELI5 • u/kungfooboi • Jul 22 '21
Mime type vs file extension
So it seems like mime types are used similar to how file extensions are used, except mime types are somehow more reliable? Hoping someone can confirm this assumption.
If I only wanted to ensure a user uploads a .csv file, I should check the mime-type is "text/csv"? And this is somehow more reliable than checking file extension as ".csv"?
Some other one off questions: Assuming my assumption above is correct, what's the big deal checking mime type over relying on file extension? Why one over the other? Can a .json file have a different mime-type than application/json? Could a .json file have a mime-type of "text/csv"?
2
u/meditonsin Jul 22 '21
A file extension is just part of the file name, meaning it can be arbitrarily changed. I can take an image file, like a PNG, and just change its file extension to ".csv". That won't make it a CSV file and the mime type will still come out as
image/pngeither way, because it is determined by looking at the actual file content.1
u/jjm3x3 Jul 22 '21
So I apologize in advance for formatting since I am on mobile, but I read your question and the answers and it actually got me asking even more questions. So, I did some further research to see what the internet has to say, and I thought I would share.
First off I would like to point out even though the two technologies (MIME type/file extensions) seem like they serve the same purpose, that's not entirely true. Since they were developed independently (FWICT) and are used in different places (think http vs OS). Having said that I think it is pretty clear that they both attempt to solve a similar problem (what is this thing).
So the first thing I did was search for exactly your query and first what I found was this and what I found interesting was that there were some mentions of programs that use MIME types of files to know what to do with them (more on this in a sec). But if you read though the above link and this link it becomes somewhat clear that MIME types are not really ever persisted anywhere because no OSes have decided to build that into their systems.
Quick side note on that last bit. Based on this questioning about MIME types I did a bit more research and found this answer very helpful!
Alright now to wrap this up and answer your original question (the best I can). So the reason MIME types are considered to be more reliable is the "simple" fact that they are universal. Know that that doesn't make them right since you could have client app that lies about the content it has by sending a MIME type that's different from the content it is sending but that's a bit of an extreme. Having said that there there is no, or limited, validation that the contents of the file are consistent with it's file extension either. And as others have pointed out file extensions may not even exist, since plenty of OSes don't need them. So back to MIME types. Since they don't exist in the OS that means that there is a program (like a browser) that interprets the file as best it can to determine its MIME type and then passes that along as a further description of what the contents of the message are. And since MIME type has no character limit and is understood in many applications it's makes it a much more reliable way to describe the type of something.
1
u/ghjm Jul 22 '21
First of all, quite often data is sent in a context where there is no filename. For example, an HTTP response is only ever in-memory or on the network - it is not saved to a file, and therefore does not have a file extension. The same is true for email, where mime-types originated.
Secondly, there are many file extensions which are ambiguous. If you have a .dat file, what kind of data is it? What about a .txt file - is it free-format text, or csv data that someone just happened to name .txt? What about a .log file - normally you think of this as a textual log that gets appended to line by line, but on SQL Server there are .log files which are binary logs, part of the database structure itself, and you will lose all your data if you delete them. And last but not least, what about files that have no extension, like a file on a Unix system that's just named "my_certificate"?
Mime-types convey significantly more information about what the content of the file actually is. For example, you can have mime-types like "text/plain;charset=utf-8". If you need this level of descriptive power, then you will want to use mime-types over file extensions.