finfo_open

finfo::__construct

(PHP >= 5.3.0, PHP 7, PHP 8, PECL fileinfo >= 0.1.0)

finfo_open -- finfo::__construct创建新 finfo 实例

说明

过程化风格

finfo_open(int $flags = FILEINFO_NONE, ?string $magic_database = null): finfo|false

面向对象风格(构造器):

public finfo::__construct(int $flags = FILEINFO_NONE, ?string $magic_database = null)

本函数打开一个魔数数据库并且返回它的实例。

参数

flags

一个 Fileinfo 常量 或多个 Fileinfo 常量 进行逻辑或运算。

magic_database

魔数数据库文件名称, 通常是 /path/to/magic.mime。 如果未指定,则使用 MAGIC 环境变量。 如果未指定此环境变量, 则使用 PHP 绑定的魔数数据库。

传入 null 或者空字符串,等同于使用默认值。

返回值

(仅适用于过程化风格) 成功时返回 finfo 实例, 或者在失败时返回 false

更新日志

版本 说明
8.1.0 现在返回 finfo 实例,之前返回 resource
8.0.3 magic_database 现在可以为 null。

示例

示例 #1 面向对象风格

<?php
$finfo
= new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // 返回 mime 类型

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

?>

示例 #2 过程化风格

<?php
$finfo
= finfo_open(FILEINFO_MIME, "/usr/share/misc/magic"); // 返回 mime 类型

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

/* 获取指定文件的 mime 类型 */
$filename = "/usr/local/something.txt";
echo
finfo_file($finfo, $filename);

/* 关闭资源 */
finfo_close($finfo);
?>

以上示例会输出:

text/plain; charset=us-ascii

注释

注意:

通常来说,除非特别需要自定义的魔数数据库。否则使用绑定的魔数数据库(不设置 magic_fileMAGIC 环境变量)是最好的做法。

参见

add a note

User Contributed Notes 10 notes

up
3
dario dot borreguero at gmail dot com
15 years ago
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.
up
19
illusivefingers at gmail dot com
12 years ago
I am running Windows 7 with Apache. It took hours to figure out why it was not working.

First, enable the php_fileinfo.dll extension in you php.ini. You'll also need the four magic files that are found in the following library:

http://sourceforge.net/projects/gnuwin32/files/file/4.23/file-4.23-bin.zip/download

An environment variable or a direct path to the file named "magic" is necessary, without any extension.

Then, make sure that xdebug is either turned off or set the ini error_reporting to not display notices or warnings for the script.

Hope this saves someone a few hours of frustration!
up
8
Anonymous
10 years ago
For most common image files:
<?php
function minimime($fname) {
$fh=fopen($fname,'rb');
if (
$fh) {
$bytes6=fread($fh,6);
fclose($fh);
if (
$bytes6===false) return false;
if (
substr($bytes6,0,3)=="\xff\xd8\xff") return 'image/jpeg';
if (
$bytes6=="\x89PNG\x0d\x0a") return 'image/png';
if (
$bytes6=="GIF87a" || $bytes6=="GIF89a") return 'image/gif';
return
'application/octet-stream';
}
return
false;
}
?>
up
1
olivier dot berger at it-sudparis dot eu
13 years ago
On my Debian squeeze system, the path needed is like :
<?php
$finfo
= new finfo(FILEINFO_MIME, "/usr/share/misc/magic.mgc");
?>
up
0
php at brudaswen dot de
15 years ago
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.
up
0
php at kingsquare dot nl
16 years ago
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);
?>
up
0
ian at createanet dot co dot uk
16 years ago
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
up
-1
mark at dynom dot nl
15 years ago
It seems there is quite some inconsistency in distributions and loading of magic files.

On Archlinux, the file is located here:
/usr/share/misc/file/magic.mgc

But this:

<?php
$fi
= new finfo(FILEINFO_MIME, '/usr/share/misc/file/magic');
$fi->file('/tmp/fubar.txt');
?>

Actually segfaults, where if I type the full name (including the file extension:)

<?php
$fi
= new finfo(FILEINFO_MIME, '/usr/share/misc/file/magic.mgc'); // added ".mgc"
$fi->file('/tmp/fubar.txt');
?>

It works as expected, so I guess something goes wrong with "A .mime and/or .mgc suffix is added if needed."
up
-4
tularis at php dot net
16 years ago
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 \!
up
-8
franssen dot roland at gmail dot com
13 years ago
Notice FileInfo::__construct() has strange behavior in PHP < 5.3.1 when a 2nd parameter is set explicitly, e.g.;

<?php
$fileInfo
= new finfo(FILEINFO_MIME, null);
?>

Expected result:
----------------
object(finfo)#2 (0) { }

Actual result:
--------------
Warning: finfo::finfo(): Failed to load magic database at ''. in *** on line ***
object(finfo)#2 (0) { }

See http://bugs.php.net/bug.php?id=51732
To Top