note 71176 added to function.mime-content-type
| From: | mathew at lifeart dot net dot au | Date: | Wed, 15 Nov 2006 06:27:18 +0000 |
| Subject: | note 71176 added to function.mime-content-type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119736@lists.php.net to get a copy of this message | ||
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
----
Server IP: 66.163.161.117
Probable Submitter: 202.161.21.91
----
Manual Page -- http://www.php.net/manual/en/function.mime-content-type.php
Edit -- https://master.php.net/note/edit/71176
Del: integrated -- https://master.php.net/note/delete/71176/integrated
Del: useless -- https://master.php.net/note/delete/71176/useless
Del: bad code -- https://master.php.net/note/delete/71176/bad+code
Del: spam -- https://master.php.net/note/delete/71176/spam
Del: non-english -- https://master.php.net/note/delete/71176/non-english
Del: in docs -- https://master.php.net/note/delete/71176/in+docs
Del: other reasons-- https://master.php.net/note/delete/71176
Reject -- https://master.php.net/note/reject/71176
Search -- https://master.php.net/manage/user-notes.php