Version: 1.3
Type: Class
Category: HTTP
License: GNU General Public License
Description: A small PHP-class that gives you access to the mime.types file that comes e.g. with apache. It is useful if you want to set the "Content-type" header before you output a file whose type is only known by its extension.
<?php /*
**
** mime_types.inc.php
**
** (c) 2002 peppermind Network Neue Medien, www.peppermind.de
**
** Daniel Boesswetter, boesswetter@peppermind.de, Thu May 23 12:40:52 CEST 2002
**
**
** <LEGAL BLURB>
** 1. this software is distributed free of charge (send me a mail if you like it :)
** 2. use at your own risk
** </LEGAL BLURB>
**
**
** $Log: mime_types.inc.php,v $
** Revision 1.3 2002/05/27 12:44:20 bos
** added some comments
**
** Revision 1.2 2002/05/24 09:40:50 bos
** added the default behaviour if no filename is specified: try to
** find apache's server root via the phpinfo command and
**
** Revision 1.1 2002/05/23 12:09:14 bos
** simple class for accessing apaches mime.types file
**
**
** this class provides access to the contents of the mime.types file as
** used by apache. it is based on the assumption, that a mime-type can
** have multiple associated file-extensions, but one file extension
** is only associated with one mime-type.
**
** mime.types is assumed to have one mime-type per line (with no leading
** whitespace), followed by whitespace-separated filename-extensions
** (usually without dots).
**
** todo:
** - some error-handling would be nice (e.g. file not found)
** - optimization by using global variables or class variables to
** avoid multiple parsing of the same file in a single process (page)
** - optimize the documentation :)
**
*/
if (!defined("mime_types.inc.php")):
define("mime_types.inc.php", true);
class mime_types {
/*
constructor:
specify a filename, if omitted will try to find it in apache's
server-root (by using phpinfo, see below)
*/
function mime_types( $filename="" ) {
if ( $filename )
$this->_filename = $filename;
else
$this->_filename = $this->_get_default_filename();
$this->_initialized = false;
}
/*
return the mime-type for a given file-extension
*/
function type_by_extension( $ext ) {
if ( !$this->_initialized ) $this->_initialize();
return $this->_ext2type[$ext];
}
/*
return an array of file-extensions for a given type
*/
function extensions_by_type( $type ) {
if ( !$this->_initialized ) $this->_initialize();
return $this->_type2ext[$type];
}
/*
array of known mime-types
*/
function known_types() {
if ( !$this->_initialized ) $this->_initialize();
return array_keys( $this->_type2ext );
}
/*
array of known file-extensions
*/
function known_extensions() {
if ( !$this->_initialized ) $this->_initialize();
return array_keys( $this->_ext2type );
}
/*
returns a human-readable dump of the internal state of this object
*/
function dump() {
if ( !$this->_initialized ) $this->_initialize();
ob_start();
echo "_filename=".$this->_filename."\n";
echo "_ext2type:\n";
print_r( $this->_ext2type );
echo "_type2ext:\n";
print_r( $this->_type2ext );
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
/*
internal: read file and parse the contents
*/
function _initialize() {
$lines = file( $this->_filename );
$this->_ext2type = array();
$this->_type2ext = array();
foreach ( $lines as $line ) {
if ( preg_match( "/^\s*\#|^\s*$/", $line ) ) continue;
$line = chop( $line );
$exts = preg_split( "/\s+/", $line );
$type = array_shift( $exts );
$this->_type2ext[$type] = $exts;
foreach ( $exts as $ext ) {
$this->_ext2type[$ext] = $type;
}
}
$this->_initialized = true;
}
/*
try to find the servers mime.types (ugly, but it works)
*/
function _get_default_filename() {
/**
capture the output of phpinfo(8) and find the table entry
called "Server Root". mime.types usually resides
under conf/mime.type
FIXME: this works only for apache!!!
*/
ob_start();
phpinfo(8);
$text = ob_get_contents();
ob_end_clean();
preg_match( "/Server Root(<[^>]*>)*([^<]+)/", $text, $matches );
return $matches[2]."/conf/mime.types";
}
}
endif;
?>