File Formats and Extensions
Level 1 · Files and formats
What a file format is, and why the end of a file name is only a hint. How a label tells a browser what kind of file it got.
Course contentsLesson 1 of 11
Level 1 · Files and formats
Level 2 · Data formats
Level 3 · Config and docs files
Level 4 · Characters and escapes
Level 5 · IDs, dates and patterns
After this lesson you can
- Say what a file format is
- Explain why renaming a file does not change it
- Read a media type like image/png
10 mincovers 3 glossary entries
What a file format is, and what the ending tells you
A file format is the set of rules for how a file stores its data: what each byte means and in what order. The file extension is the short ending after the last dot, like .png or .json. It is only a hint. Every PNG picture starts with the same 8 bytes, whatever the file is called.
A program needs to know the format to read a file. A photo app knows how PNG lays out its colours. A music app knows how a song file is laid out. Hand a program a format it does not know, and it shows an error or a mess.
Your computer uses the extension to pick an app. Double-click report.pdf, and the PDF reader opens it. That is all the extension does. The bytes inside decide what the file really is.
So renaming a file does not change its format. Rename photo.png to photo.jpg, and the bytes are still a PNG. Some apps open it anyway, by reading the first bytes. Others trust the name and refuse.
To change a format, you convert the file. A tool reads the old format and writes the data again in the new one. That is what Save As does when you pick a new type.
On the web, a file has no name ending the browser can trust. So the sender adds a label, the media type, also called a MIME type. It has two parts and a slash, like text/html or image/png. The browser reads it to decide whether to show a page, play a video or save a file.
A wrong media type causes odd bugs. A script sent as text/plain may not run at all. A page sent as a download is saved, not shown. When a file acts strangely in a browser, check the label it came with.
$ file --mime-type icon.png
icon.png: image/png
$ head -c 8 icon.png | xxd
00000000: 8950 4e47 0d0a 1a0a .PNG....Worked example
Lucy renames a photo
A form asks Lucy for a JPG, and her photo is a PNG of 2 megabytes. She renames it to photo.jpg, and the form rejects it as a broken file.
She opens it in a photo app and uses Save As with JPG picked. The new file is 400 kilobytes, and the form takes it.
The words for this lesson3
Check yourself
If I rename photo.png to photo.jpg, is it a JPG now?
Finished this lesson?
Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.