Reasons to Change the File Extension of a File

Share
In this article, I'll list some reasons you may have to change the file extension of a file. Normally, there is no reason to do this at all, since the file will have the appropriate extension, because there is no reason for it to have the wrong extension, so I think it's interesting to consider when that may not be the case.

Bypassing File Extension Filters

In some cases, a software may filter which files it accepts according to their extension, even though the actual file types it supports is greater than the file extensions it allows to pass through. This may occur when the software relies on third-party libraries that support a greater variety of formats than the software developer intends to support, or even knows about.

A practical example of this is RPG Maker. In some versions of this software, users can only import images in the with the PNG extension, even thought these versions use web technologies (Electron), which means anything a web browser can load, it should be able to load as well, including JPG, GIF, BMP, and even WebP. In this case, users are able to work around the filter to import a JPG file in RPG maker simply by renaming the file to have a .png extension instead of a .jpg extension.

The editor itself doesn't support jpg files, however all export options end up running the game using a web browser. The desktop clients uses Node.js and NW.js, which provides the user with a stripped-down version of Google Chrome version 41. Since chrome supports jpeg files, they should appear fine in the game.

To get the editor to let you pick the files, you'll have to rename them to *.jpg.png so that you can still tell they're jpegs, but the engine thinks they're pngs.

https://forums.rpgmakerweb.com/index.php?threads/only-supports-png-files.47333/#post-472498, "Oct 26, 2015" (accessed 2025-02-15)

In general, changing the file extension of an image, video, or audio file won't actually affect the programs that load the files, only the applications' graphical user interfaces. That's because the programs that load the files generally don't rely on the file extension to guess the format of the file. Instead, there are ways to derive the file type from the data of the file. For example, every PNG file starts with the same eight bytes.

The first eight bytes of a PNG file always contain the following values:

   (decimal)              137  80  78  71  13  10  26  10
   (hexadecimal)           89  50  4e  47  0d  0a  1a  0a
   (ASCII C notation)    \211   P   N   G  \r  \n \032 \n
http://www.libpng.org/pub/png/spec/1.2/PNG-Rationale.html (accessed 2025-02-15)

This means if you see these first 8 bytes in a file, you know that's very likely a PNG file, specially in the context of image files. This is called a "magic number" and many formats have something similar, allowing lower level programs to tell them apart once you actually open the file.

Higher level programs like applications generally do not open every single file even to inspect their first bytes just to guess what the type of the file is going to be. Instead, they use the file extension, which is going to be the right extension almost every time, saving them the trouble. After all, why would the file extension be wrong?

Observation: normally, the default filter of an application's open file dialog is "All Supported File Types," which is file extension based, but there will be an option called "All Files (*.*)" that simply displays all files regardless of extension. Sometimes it appears as *.*, which implies a dot (.) character and therefore some extension will be required, but in practice it just displays all the files including those without any file extension.

Downloads with Missing Extensions

Sometimes, if you download something from the Internet, it won't have a file extension because its URL didn't have a file extension.

Generally speaking, file extensions don't mean anything in URLs. On Windows and Linux, the file extension is used to tell the file type of a file most of the time. On the web, the file type, or, strictly speaking, media type of a resource located by an URL is determined by the Content-Type header found in the HTTP response sent by the web server to the web browser.

Most web developers never really have to deal with these headers. Instead, they simply use a web server software that automatically sets the header based on the file extension, such as Apache. For example, if you have a photo.png file on your web server, Apache will automatically send the Content-Type: image/png HTTP header when its URL is accessed.

However, some more complicated websites do not work this way. Instead of serving all static files from a single computer running Apache, they may use a third-party content delivery network (CDN) to host their files. This means that when a file is uploaded to the website, the website sends the file to the CDN, and one of the things the CDN needs to know about is what is the type of the file so that it can tell the clients when that file is accessed later

The CDN may generate a random-looking URL for that file, and this URL may not have an extension at all. In this case, you get an URL without extension that still has the appropriate media type in its HTTP header, so everything works just fine in the browser, until you try to download something to save in your PC, then you may end up with an image that works fine in the web browser, but that has no image file type when you look at it in your downloads folder.

In this case, you could fix it simply by adding the appropriate file extension, or, in some cases, just by adding any related extension at all, since most low level programs can figure out file types from magic numbers. For example, if you downloaded an image without extension and you don't know whether it's PNG or JPG, you can just add .png and it should work even you got that wrong. Similarly, if you download a video file you can just give it the .mp4 extension and it should work. If it doesn't, I guess you'll have to try a different extension until it works.

Downloads with Strange Extensions

One peculiar problem one may encounter is that trying to download a video from Imgur or Tumblr may get you a file with a .gifv extension that no video player or video editor supports. This format may sound like it's a GIF image, but it's not. Officially called "GIF video," this is no special or unique format, but literally an .mp4 or .webm video file with a different file extension. If you change the file extension to .mp4, it will work on your video player or video editor just fine.

Documents that are Zipped Folders

Some documents are actually zipped folders in disguise, the disguise being a file extension other than .zip. This is the case with many major document types, including those of LibreOffice Writer and Calc.

For example, let's say you have an OpenDocument Spreadsheet where you rated different flavors of ice cream for some reason, and this means you took photos of ice cream cones and drag and dropped them into Calc. You had a file called strawberry.jpg for the strawberry flavor, but you accidentally deleted that file, and you didn't have a backup, meaning the only copy left of your precious strawberry ice cream cone photo is contained inside the spreadsheet. How do you get it out?

You can actually just rename the file from Ice Cream.ods to Ice Cream.zip, and unzip it, then you'll find your strawberry.jpg inside a Pictures folder, together with all other image files you embedded inside the spreadsheet, although it won't have the same filename anymore, but a filename generated by LibreOffice Calc.

Observation: some archivers like 7zip can add an option to the context menu on File Explorer that just lets you (try to) extract any file as if it were a zipped folder. Most of the time this is useless and just a waste of space, but in cases like this it can be used instead.

Source Code Files

The last reason is the low-hanging fruit: changing the file extension of a source code file to match its source code language.

All source code files (except the esoteric ones) are plain text files, which means that their extension would be just .txt. However, source code editors provide syntax highlighting based on the language of the source code, and there is no reasonable way to determine the language from the content alone, meaning that the developer must tell the editor somehow what language are they writing.

The simplest way for them to provide this information is to set the file extension of the source code file to reflect the language contained inside the file. Since there are dozens of languages, that means there are dozens of source code extensions, and one new extension is created every time a new language shows up.

For example, we have .js files for Javascript, and .ts for Typescript. Typescript is a superset of Javascript, meaning that any syntactic valid .js file is a syntactic valid .ts file. This fact alone means there would be no way for the editor to know if a file is Javascript or Typescript if the developer didn't explicitly inform the program somehow.

Another extreme common case is XML. XML is a language meant to be eXtensive, meaning that anybody could employ it to create their own formats, and so there are several formats derived from XML, such as XHTML, RSS, ATOM, and countless others. To distinguish them, file extensions are used: .xml, .xhtml, .rss, and .atom.

Making Programs Find Files

In some cases, a game for example may look for a file that has a specific file extension, such as .ini, but a text editor doesn't save files with .ini extension by default, they save it with .txt, so you may have to save it as .txt first and then rename it to .ini.

Normally you should be able to just type options.ini, for example, in the save file dialog to save it with the intended extension, but this is a trick I assume most people don't know about when they rarely have to deal with this.

If you're a programmer, this is really an indispensable skill, as some programs even expect files to be just a file extension, such as .gitignore used by git. These are called dotfiles and they're more common on Linux than on Windows. In fact, in the past, you weren't even able to rename a file to a filename that starts with a dot on Windows via the File Explorer, you had to use the terminal.

Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments