PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

finfo_set_flags> <finfo_file
Last updated: Fri, 10 Oct 2008

view this page in

finfo_open

finfo->__construct()

(PECL fileinfo:0.1-1.0.4)

finfo_open -- finfo->__construct()Crée une nouvelle ressource fileinfo

Description

Style procédural :

resource finfo_open ([ int $options [, string $arg ]] )

Style orienté objet :

finfo
__construct ([ int $options [, string $magic_file ]] )

Cette fonction ouvre une base de données magique et retourne sa ressource.

Liste de paramètres

options

Une ou une union de plusieurs constantes Fileinfo.

magic_file

Nom de fichier d'une base de données magique, normalement quelque chose comme /path/to/magic.mime. Si non spécifié, la variable d'environnement MAGIC est utilisée. Si cette variable n'est pas fixée non plus, /usr/share/misc/magic est utilisé. L'extension .mime et/ou .mgc est ajoutée si approprié.

Valeurs de retour

Retourne une ressource de base de données magique en cas de succès ou FALSE en cas d'échec.

Exemples

Exemple #1 Style orienté objet

<?php
$finfo 
= new finfo(FILEINFO_MIME"/usr/share/misc/magic"); // Retourne le type mime

if (!$finfo) {
    echo 
"Échec de l'ouverture de la base de données fileinfo";
    exit();
}

/* Récupère le mime-type d'un fichier spécifique */
$filename "/usr/local/something.txt";
echo 
$finfo->file($filename);

/* Fermeture de la connexion */
$finfo->close();
?>

Exemple #2 Style procédural

<?php
$finfo 
finfo_open(FILEINFO_MIME"/usr/share/misc/magic"); // Retourne le type mime

if (!$finfo) {
    echo 
"Échec de l'ouverture de la base de données fileinfo";
    exit();
}

/* Récupère le mime-type d'un fichier spécifique */
$filename "/usr/local/something.txt";
echo 
finfo_file($finfo$filename);

/* Fermeture de la connexion */
finfo_close($finfo);
?>

L'exemple ci-dessus va afficher :

text/plain

Voir aussi



finfo_set_flags> <finfo_file
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
finfo_open
php at brudaswen dot de
15-Sep-2008 03:13
Since it costed me some time to find the needed magic database files for Windows, just a hint:

The last release of the GnuWin32 project with both needed files (magic and magic.mime) currently was "file-4.23".
All releases after 4.23 to 4.25-1 did not contain both needed files.

Hope that helps.
dario dot borreguero at gmail dot com
16-Apr-2008 03:15
Platform: WinXP-SP2, PHP5.2.5, MySQL 5.0, Apache 2.2.8

After reading former notes, I wasn't able to load my magic database: 'magic.mime' file (donwloaded from GnuWin32 project, zipped with the binary files v4.21). I always got an invalid $finfo object or finfo_open(...) returned FALSE.

In order to be able to load the 'magic.mime' file, Fileinfo library (bundled in PHP5.2.5) also requires 'magic' file.

For example:
1. Database:
  c:\php\magic.mime
  c:\php\magic

2. PHP Code:
<?php
  $filname
= 'c:\php\php.ini';
 
$finfo = new finfo(FILEINFO_MIME, 'c:\php\magic');
  if (!
$finfo) return false;
  echo
$finfo->file($filename);
?>

For further info see: http://pecl.php.net/bugs/bug.php?id=7555

Pay attention to comments added by 'christophe dot charron dot xul at gmail dot com'

NOTE: Before upgrading to PHP5.2.5, I was working with PHP5.2.1 and it only required 'magic.mime' file.
php at kingsquare dot nl
23-Jan-2008 07:41
The current version (1.04) doesnt support a different mime.magic database than the server default.

(the documentation is thus not correct)
Ubuntu default location is '/usr/share/file/magic'. In order for the examples to work all finfo_open()-commands must be issued with the extra location accordingly:
<?php
$file
= "/path/to/file.jpg";
$handle = finfo_open(FILEINFO_MIME, '/usr/share/file/magic');
$mime_type = finfo_file($handle,$file);
?>
ian at createanet dot co dot uk
02-Nov-2007 09:50
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
aldoh at cableonda dot net
19-Sep-2007 06:43
Here is the exact link for Win32user to the file binary package:

http://sourceforge.net/project/showfiles.php?
group_id=23617&package_id=18878
tularis at php dot net
06-May-2007 12:55
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 \!

finfo_set_flags> <finfo_file
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites