MIME-types

What is MIME-type

Message 1. Modified by Trollope October, 19, 2016 To the top ↑
Data are transmitted as binary code in the Internet. When a clent (usually a browser) get data from a web-server, it has to know the kind of received data (html file, image file, executable file, video file, etc.). The MIME-type is transmitted with data and describes its kind.
MIME is acronym of the Multipurpose Internet Mail Extensions.
The MIME-type is a string. It has two parts, separated by a slash: a major type and a minor type. For example text/html, image/jpeg.
In HTTP, the Content-Type header contains the MIME-type.

Internet Assigned Numbers Authority (IANA)

Message 2. Modified by Trollope October, 19, 2016 To the top ↑
The IANA (Internet Assigned Numbers Authority) registers and manages MIME-types. See all registered MIME-types at http://www.iana.org/assignments/media-types/media-types.xhtml.

Popular MIME-types

Message 3. Modified by Trollope October, 19, 2016 To the top ↑
MIME-typeDescription
application/javascriptJavaScript files
application/xmlSitemap.xml file
image/gifImage files in GIF format
image/jpegImage files in JPEG format
image/pngImage files in PNG format
image/vnd.microsoft.iconImage files in ICO format,
Favicon.ico file
text/cssStyle sheets
text/htmlHTML files

Example in PHP

Message 4. Modified by Middlebrook October, 19, 2016 To the top ↑
Function getContentType recognizes the MIME-type by URL using regular expressions. This function is a part of code of this site. Default value defined by the most common regular expression (".*") is "text/html".
function getContentType($url) {

	// Structure: $contentTypes[Regular Expression] = MIME-type
	$contentTypes[".*"] = "text/html";
	$contentTypes[".css$"] = "text/css";
	$contentTypes[".gif$"] = "image/gif";
	$contentTypes[".ico$"] = "image/vnd.microsoft.icon";
	$contentTypes[".js$"] = "application/javascript";
	$contentTypes[".jpg$"] = "image/jpeg";
	$contentTypes[".png$"] = "image/png";
	$contentTypes["^/sitemap.xml$"] = "application/xml";

	foreach ($contentTypes as $regex => $mimeType) {
		if (ereg($regex, $url)) {
			$contentType = $mimeType;
		}
	}

	return $contentType;
}
 
Some code calls the getContentType function to form Content-Type HTTP-header.
$contentType = getContentType($url);
header("Content-Type: $contentType");
 
Do you want to add or ask something? Contact us!