Last Comments

snorkel (Data Warehouse / …): Oracle seems like a huge …
frak (Article Bot is fi…): Well thanks for being the…
random (Article Bot is fi…): Curious Statement from th…
frak (Article Bot Spyin…): For the answer see this u…
Bob (Pivot 1.30 Alpha.…): And this is what a commen…

Archives

01 Nov - 30 Nov 2007
01 Jun - 30 Jun 2007
01 Dec - 31 Dec 2006
01 Nov - 30 Nov 2006
01 Jul - 31 Jul 2006
01 Jun - 30 Jun 2006
01 Apr - 30 Apr 2006
01 Mar - 31 Mar 2006
01 Feb - 28 Feb 2006
01 Jan - 31 Jan 2006
01 Dec - 31 Dec 2005
01 Oct - 31 Oct 2005
01 Sep - 30 Sep 2005
01 Aug - 31 Aug 2005
01 Feb - 28 Feb 2005
01 Jan - 31 Jan 2005

Miscellany

Powered by Pivot - 1.40.5: 'Dreadwind' 
XML: RSS Feed 
XML: Atom Feed 

« Internet Marketers On… | Home | Finally a Keyword Com… »

PHP mime_content_type() Alternative avoiding PECL and PHPCompat

Okay - its been a long time between posts (that's code for very busy).  The following is something I came accross while installing a membership system on somebody elses server.  Aside from old PHP4, there were missing libraries...  this is the result.

If mime_content_type is unavailable, and so is the PECL FileInfo library and so is the shell (in which case you are pretty annoyed by now) here is an alternative that falls back on the file extension - instead of 'magic'.

I took the mime list basically and used that for the extension check.  note that unlike 'magic' file detection this will get fooled by a bad extension.  So a PDF file called file.zip will be reported as an 'application/zip' file.

Like the PHP Compat library, this falls back to 'application/octet-stream'

<?php
if (!function_exists('mime_content_type')) {
function mime_content_type($filename) {
$idx = strtolower(end( explode( '.', $filename )) );
$mimet = array( 'ai' =>'application/postscript',
'aif' =>'audio/x-aiff',
'aifc' =>'audio/x-aiff',
'aiff' =>'audio/x-aiff',
'asc' =>'text/plain',
'atom' =>'application/atom+xml',
'avi' =>'video/x-msvideo',
'bcpio' =>'application/x-bcpio',
'bmp' =>'image/bmp',
'cdf' =>'application/x-netcdf',
'cgm' =>'image/cgm',
'cpio' =>'application/x-cpio',
'cpt' =>'application/mac-compactpro',
'crl' =>'application/x-pkcs7-crl',
'crt' =>'application/x-x509-ca-cert',
'csh' =>'application/x-csh',
'css' =>'text/css',
'dcr' =>'application/x-director',
'dir' =>'application/x-director',
'djv' =>'image/vnd.djvu',
'djvu' =>'image/vnd.djvu',
'doc' =>'application/msword',
'dtd' =>'application/xml-dtd',
'dvi' =>'application/x-dvi',
'dxr' =>'application/x-director',
'eps' =>'application/postscript',
'etx' =>'text/x-setext',
'ez' =>'application/andrew-inset',
'gif' =>'image/gif',
'gram' =>'application/srgs',
'grxml' =>'application/srgs+xml',
'gtar' =>'application/x-gtar',
'hdf' =>'application/x-hdf',
'hqx' =>'application/mac-binhex40',
'html' =>'text/html',
'html' =>'text/html',
'ice' =>'x-conference/x-cooltalk',
'ico' =>'image/x-icon',
'ics' =>'text/calendar',
'ief' =>'image/ief',
'ifb' =>'text/calendar',
'iges' =>'model/iges',
'igs' =>'model/iges',
'jpe' =>'image/jpeg',
'jpeg' =>'image/jpeg',
'jpg' =>'image/jpeg',
'js' =>'application/x-javascript',
'kar' =>'audio/midi',
'latex' =>'application/x-latex',
'm3u' =>'audio/x-mpegurl',
'man' =>'application/x-troff-man',
'mathml' =>'application/mathml+xml',
'me' =>'application/x-troff-me',
'mesh' =>'model/mesh',
'mid' =>'audio/midi',
'midi' =>'audio/midi',
'mif' =>'application/vnd.mif',
'mov' =>'video/quicktime',
'movie' =>'video/x-sgi-movie',
'mp2' =>'audio/mpeg',
'mp3' =>'audio/mpeg',
'mpe' =>'video/mpeg',
'mpeg' =>'video/mpeg',
'mpg' =>'video/mpeg',
'mpga' =>'audio/mpeg',
'ms' =>'application/x-troff-ms',
'msh' =>'model/mesh',
'mxu m4u' =>'video/vnd.mpegurl',
'nc' =>'application/x-netcdf',
'oda' =>'application/oda',
'ogg' =>'application/ogg',
'pbm' =>'image/x-portable-bitmap',
'pdb' =>'chemical/x-pdb',
'pdf' =>'application/pdf',
'pgm' =>'image/x-portable-graymap',
'pgn' =>'application/x-chess-pgn',
'php' =>'application/x-httpd-php',
'php4' =>'application/x-httpd-php',
'php3' =>'application/x-httpd-php',
'phtml' =>'application/x-httpd-php',
'phps' =>'application/x-httpd-php-source',
'png' =>'image/png',
'pnm' =>'image/x-portable-anymap',
'ppm' =>'image/x-portable-pixmap',
'ppt' =>'application/vnd.ms-powerpoint',
'ps' =>'application/postscript',
'qt' =>'video/quicktime',
'ra' =>'audio/x-pn-realaudio',
'ram' =>'audio/x-pn-realaudio',
'ras' =>'image/x-cmu-raster',
'rdf' =>'application/rdf+xml',
'rgb' =>'image/x-rgb',
'rm' =>'application/vnd.rn-realmedia',
'roff' =>'application/x-troff',
'rtf' =>'text/rtf',
'rtx' =>'text/richtext',
'sgm' =>'text/sgml',
'sgml' =>'text/sgml',
'sh' =>'application/x-sh',
'shar' =>'application/x-shar',
'shtml' =>'text/html',
'silo' =>'model/mesh',
'sit' =>'application/x-stuffit',
'skd' =>'application/x-koan',
'skm' =>'application/x-koan',
'skp' =>'application/x-koan',
'skt' =>'application/x-koan',
'smi' =>'application/smil',
'smil' =>'application/smil',
'snd' =>'audio/basic',
'spl' =>'application/x-futuresplash',
'src' =>'application/x-wais-source',
'sv4cpio' =>'application/x-sv4cpio',
'sv4crc' =>'application/x-sv4crc',
'svg' =>'image/svg+xml',
'swf' =>'application/x-shockwave-flash',
't' =>'application/x-troff',
'tar' =>'application/x-tar',
'tcl' =>'application/x-tcl',
'tex' =>'application/x-tex',
'texi' =>'application/x-texinfo',
'texinfo' =>'application/x-texinfo',
'tgz' =>'application/x-tar',
'tif' =>'image/tiff',
'tiff' =>'image/tiff',
'tr' =>'application/x-troff',
'tsv' =>'text/tab-separated-values',
'txt' =>'text/plain',
'ustar' =>'application/x-ustar',
'vcd' =>'application/x-cdlink',
'vrml' =>'model/vrml',
'vxml' =>'application/voicexml+xml',
'wav' =>'audio/x-wav',
'wbmp' =>'image/vnd.wap.wbmp',
'wbxml' =>'application/vnd.wap.wbxml',
'wml' =>'text/vnd.wap.wml',
'wmlc' =>'application/vnd.wap.wmlc',
'wmlc' =>'application/vnd.wap.wmlc',
'wmls' =>'text/vnd.wap.wmlscript',
'wmlsc' =>'application/vnd.wap.wmlscriptc',
'wmlsc' =>'application/vnd.wap.wmlscriptc',
'wrl' =>'model/vrml',
'xbm' =>'image/x-xbitmap',
'xht' =>'application/xhtml+xml',
'xhtml' =>'application/xhtml+xml',
'xls' =>'application/vnd.ms-excel',
'xml xsl' =>'application/xml',
'xpm' =>'image/x-xpixmap',
'xslt' =>'application/xslt+xml',
'xul' =>'application/vnd.mozilla.xul+xml',
'xwd' =>'image/x-xwindowdump',
'xyz' =>'chemical/x-xyz',
'zip' =>'application/zip'
);

if (isset( $mimet[$idx] )) {
return $mimet[$idx];
} else {
return 'application/octet-stream';
}
}
}
?>


No comments:


No trackbacks:

Trackback link:

Please enable javascript to generate a trackback url


  
Remember personal info?

/ Textile

this is to stop spam bots causing me pain.
 

  (Register your username / Log in)

Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.