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



PHP : Function Reference : Fileinfo Functions : finfo_open

finfo_open

Create a new fileinfo resource (PECL fileinfo:0.1-1.0.4)
resource finfo_open ( [int options [, string arg]] )

Example 617. Object oriented style

<?php
$finfo
= new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // return mime type ala mimetype extension

if (!$finfo) {
   echo
"Opening fileinfo database failed";
   exit();
}

/* get mime-type for a specific file */
$filename = "/usr/local/something.txt";
echo
$finfo->file($filename);

/* close connection */
$finfo->close();
?>

Example 618. Procedural style

<?php
$finfo
= finfo_open(FILEINFO_MIME, "/usr/share/misc/magic"); // return mime type ala mimetype extension

if (!$finfo) {
   echo
"Opening fileinfo database failed";
   exit();
}

/* get mime-type for a specific file */
$filename = "/usr/local/something.txt";
echo
finfo_file($finfo, $filename);

/* close connection */
finfo_close($finfo);
?>

Code Examples / Notes » finfo_open

cs

the "close" method does not exists, as it is the default deconstructior there is no need to call it.

tularis

On Windows systems people might find that this always returns "application/x-dpkg".
There are 2 ways of solving this problem:
1. Get the mime-magic database file from GnuWin32 at <http://sourceforge.net/projects/gnuwin32/>
2. You can manually "fix" the mime-magic file by editing it and escaping all lines starting with !, thus changing each one to \!


aldoh

Here is the exact link for Win32user to the file binary package:
http://sourceforge.net/project/showfiles.php?
group_id=23617&package_id=18878


ian

Couldn't get finfo to return the mimetype in the way expected so i made a function to do it with system()
<?php
function get_mime_type($filepath) {
ob_start();
system("file -i -b {$filepath}");
$output = ob_get_clean();
$output = explode("; ",$output);
if ( is_array($output) ) {
$output = $output[0];
}
return $output;
}
?>
hope it works for other people too


Change Language


Follow Navioo On Twitter
finfo_buffer
finfo_close
finfo_file
finfo_open
finfo_set_flags
eXTReMe Tracker