Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Mimetype Functions : mime_content_type

mime_content_type

Detect MIME Content-type for a file (deprecated) (PHP 4 >= 4.3.0, PHP 5)
string mime_content_type ( string filename )

Example 1330. mime_content_type() Example

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

The above example will output:

image/gif
text/plain

Code Examples / Notes » mime_content_type

ginnsu

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"


tree2054 using hotmail

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 ) ) ) ;
  }
}
?>


y0gi

The 'file' utility fortunately is available for Windows, too:
http://gnuwin32.sourceforge.net/packages/file.htm


16-oct-2006 03: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'.

some dude

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.


sune jensen

For me mime_content_type didn't work in Linux before I added
mime_magic.magicfile = "/usr/share/magic.mime"
to php.ini (remember to find the correct path to mime.magic)


webmaster

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).


dt

<?php
if (!function_exists('mime_content_type ')) {
   function mime_content_type($filename) {
       $finfo    = finfo_open(FILEINFO_MIME);
       $mimetype = finfo_file($finfo, $filename);
       finfo_close($finfo);
       return $mimetype;
   }
}
?>


quis

<?PHP
 function qmimetype($file) {
   $ext=array_pop(explode('.',$file));
   foreach(file('/usr/local/etc/apache22/mime.types') as $line)
     if(preg_match('/^([^#]\S+)\s+.*'.$ext.'.*$/',$line,$m))
       return $m[1];
   return 'application/octet-stream';
 }
?>
Not perfect, but works good enough for me ;)


Change Language


Follow Navioo On Twitter
mime_content_type
eXTReMe Tracker