PHP Programming And MIME-Types

PHP Programs can define and declare MIME-Types for files. A “MIME-Type” is a descriptor for files identifying it with a particular application or application set. The MIME-Type for a file may sometimes be determined by the filename extension and/or the MIME-Type tag inside the file contents. The MIME-Type essentially indentifies the type of content in the file.

MIME-Types are used to associate a file with the correct application for processing. Opening movies with text programs or using a music program to listen to a spreadsheet would simply be problematic. Computers have defined lists of MIME-Types and the programs that can open and use those files. When the computer discovers a MIME-Type that is not already listed, it displays a dialog box asking you to find a suitable program, or perhaps to search the Internet for an appropriate match. Computers allow you to define and redefine which programs are to be associated with particular MIME-Types for future reference and automatic launching.

Although filename extensions may indicate the type of content in the file, it is not accurate in many cases. A rare file type may have a specific file name extensions that does associate with a single MIME-Type, and vice versa. However, there are many MIME-Types that can be associated with multiple file name extensions. Web pages come in different flavors such as PHP, ASP, CFM and HTM. They are parsed and processed by different web server technologies, yet are they really different? In fact, these file types are all defined as “text/html” and are the same MIME-Type. The MIME-Type identified the file as “web text” while the file name extensions helps the web server know which technology to use to parse the file.

PHP Programs can define and redefine the MIME-Type associated with a file. PHP Scripts may generate images on the fly, stream them from remote locations, or combine multiple file types to be distributed as a final file type.  PHP can determine the MIME-Type from a file that lacks a file name extension, and process that file accordingly. A file that is delivered by PHP may lack a file name extension, but with the correct MIME-Type, it will be displayed and correctly processed by the recipient. PHP Headers are used to declare MIME-Types, and must be delivered with other headers, prior to any other content. Scripts may be devoted to graphic generation and create an image on the fly. The script must declare the MIME-Type, stream the file contents, and then close the file stream and network connection.

Image tags in web pages have the format of <img src=”image.jpg”> with various other modifier tags like size, border, etc. A PHP script that generates an image on the fly can be requested in place of the standard image file names, such as <img src=”image_script.php”>.  This image tag is requesting a PHP file nd the browser recognizes the request, as does the web server. The PHP Program then generates or streams the image data, after declaring the correct MIME-Type. The browser then makes receipt of the MIME-Type, then the image data, and appropriately handles the data by displaying it for us as an image. If the MIME-Type declaration is missing or invalid, the browser wil lnot understand how to manage the image data and will result in an error, no image, or the browser may display the textual code streamed from the PHP Server.

Since HTM, HTML, PHP, TXT and other web formats are “text/html” MIME-Types with different file name extensions, they can be handled similarly as well as separately. Workstations may be modified similarly to web server, to adjust the way MIME-Types are handled. A PHP Programmer may want to process multiple web formats through the PHP Parser. The MIME-Type list may be accessed as a configuration file on the server and the associations changed. If the “txt/html” MIME-Type has been remapped to the server handler “application/x-httpd-php” it will be processed by the PHP Parser instead of handled as plain text. Multiple file name extensions can be mapped to the same MIME-Type and server handler. What appears on the surface to be a static HTML website, may in fact be a MySQL Database driven dynamic PHP Web Site, which has simply been remapped to non-standard MIME-Types and server handlers.

Modifications to MIME-Type maps may be used for hiding which parsing technology is being used, to enhance Network Security features, or to simply create a proprietary combination of file types and content handlers. It isn’t a good idea to modify standard MIME-Type maps unless you know what you’re doing, or the computer you modify is a test box that can be trashed. Although you can savage your MIME-Type map fairly easily, it is also easy to interfere with the computer’s ability to operate correctly. MIME-Type modifications typically disconnect your computer from the rest of the world, since it will be communicating on a different wavelength than everyone else.

On the other hand, you can remap your own file name extensions to standard MIME-Types and content handlers. If your website sold cardboard boxes, you could remap the file name extension .box to the “text/html” MIME-Type and the “application/x-httpd-php” content handlers. The result is a website in which every file name ends with .box and the users won’t necessarily see whether you are using PHP, ASP, CFM, or some other parsing technology. Of course, if here is no business cash flow advantage, remapping MIME-Types is all a waste of time.

Leave a Reply