note 71176 deleted from function.mime-content-type by colder

From: Date: Sun, 07 Jan 2007 01:01:12 +0000
Subject: note 71176 deleted from function.mime-content-type by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-121356@lists.php.net to get a copy of this message
Note Submitter: mathew at lifeart dot net dot au ---- 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', 'xyz' =>'chemical/x-xyz', 'zip' =>'application/zip' ); if (isset( $mimet[$idx] )) { return $mimet[$idx]; } else { return 'application/octet-stream'; } } } ?> NOTE: This is cut short to keep with size requirements. See the full function definition in my blog: http://frakkle.com/entry/103/php_mime_content_type_alternat Cheers, Mathew Frank

« previous php.notes (#121356) next »