mime_content_type

(PHP 4 >= 4.3.0, PHP 5)

mime_content_type -- Detect MIME Content-type for a file

Description

string mime_content_type ( string filename )

Returns the MIME content type for a file as determined by using information from the magic.mime file. Content types are returned in MIME format, like text/plain or application/octet-stream.

例子 1. mime_content_type() example

<?php
echo mime_content_type('php.gif') . "\n";
echo
mime_content_type('test.php');
?>

上例将输出:

image/gif
text/plain


add a note add a note User Contributed Notes
tree2054 using hotmail
04-Nov-2006 09:59
The correct little correction:

exec will return the mime with a newline at the end, the trim() should be called with the result of exec, not the other way around.

<?php

if ( ! function_exists ( 'mime_content_type ' ) )
{
   function
mime_content_type ( $f )
   {
       return
trim ( exec ('file -bi ' . escapeshellarg ( $f ) ) ) ;
   }
}

?>
16-Oct-2006 10:06
if you use a transparent 'spacer' GIF i've found it needs to be a around 25x25 for it to register as 'image/gif'. otherwise it's read in as 'text/plain'.
keczerad at poczta dot fm
31-Aug-2006 04:38
little correction:

<?php

if ( ! function_exists ( 'mime_content_type ' ) )
{
   function
mime_content_type ( $f )
   {
       return
exec ( trim( 'file -bi ' . escapeshellarg ( $f ) ) ) ;
   }
}

?>
jacob dot saxberg at gmail dot com
25-Aug-2006 08:06
if (!function_exists('mime_content_type')) {
   function mime_content_type ($f) {
       return trim(shell_exec('file -bi '.escapeshellarg($f)));
   }
}
sven at frontmedia dot it
06-Mar-2006 04:58
little correction:

<?php

if ( ! function_exists ( 'mime_content_type ' ) )
{
   function
mime_content_type ( $f )
   {
       return
system ( trim( 'file -bi ' . escapeshellarg ( $f ) ) ) ;
   }
}

?>
webmaster at cafe-clope dot net
23-Feb-2006 09:31
Completing <some dude AT somewhere DOT com> comment:

0 string < ? php application/x-httpd-php

and string detection on text files may fail if you check a file encoded with signed UTF-8. The UTF-8 signature is a two bytes code (0xFF 0xFE) that prepends the file in order to force UTF-8 recognition (you may check it on an hexadecimal editor).
Y0Gi
26-Jan-2006 03:13
The 'file' utility fortunately is available for Windows, too:
http://gnuwin32.sourceforge.net/packages/file.htm
some dude AT somewhere DOT com
08-Oct-2005 12:44
I added these two lines to my magic.mime file:

0 string \<?php application/x-httpd-php
0 string
<?xml text/xml

The first one may not work
if "<?php" is not at the very beginning of your file, e.g., if some HTML preceeds the first bit of PHP code. The second one should work because "<?xml" *should* be the first thing in every XML file.
priyagnair at yahoo dot com
07-Sep-2005 07:40
Quote:
>>sunfra at firenze dot net
>>29-Apr-2005 06:31

mimetype Full list Ref: http://www.duke.edu/websrv/file-extensions.html
ginnsu at arcee dot ca
09-Mar-2005 02:14
The function mime_content_type only worked for me on Microsoft Windows after I added the directive "mime_magic.debug" to my php.ini with the value of "On". The default value appears to be "Off". Exampe:

[mime_magic]
mime_magic.debug = On
mime_magic.magicfile = "c:\php\extras\magic.mime"
Cooljay
08-Nov-2004 10:22
This works on windows too. Make sure to uncomment the following in your php.ini file:
extension=php_mime_magic.dll

Also make sure to add the following line anywhere inside the php.ini file:
mime_magic.magicfile = "C:\path\to\php\install\magic.mime"

Voila working mime content types in windows :)
Sven Riedel
10-Jul-2004 09:17
Note that you will need /bin/sh as well as the file(1) program for this function to work. This is relevant when setting apache up in a chroot-environment. Took me a while to find that out.