finfo_open() - fileinfo函数(文件信息)
finfo_open()
finfo::__construct
(PHP >= 5.3.0, PECL fileinfo >= 0.1.0)
创建一个 fileinfo 资源
说明
过程化风格finfo_open([int $options= FILEINFO_NONE[,string $magic_file=
NULL
]]): resource面向对象风格(构造器):
publicfinfo::__construct([int $options= FILEINFO_NONE[,string $magic_file=NULL
]])本函数打开一个魔数数据库并且返回它的资源。
参数
$options一个Fileinfo 常量或多个Fileinfo 常量进行逻辑或运算。
$magic_file魔数数据库文件名称,通常是/path/to/magic.mime。如果未指定,则使用MAGIC环境变量。如果未指定此环境变量,则使用 PHP 绑定的魔数数据库。
传入NULL
或者空字符串,等同于使用默认值。
返回值
(仅适用于过程化风格)如果成功则返回一个表示魔数数据库的资源,或者在失败时返回FALSE
。
注释
Warning在 PHP 5.3.11 和 5.4.1 中预期的魔数数据库格式发生了变动,所以,内置的魔数数据库被更新。如果使用了外部魔数数据库,可能会由于格式不同导致读取失败。同时,一些 mime 类型的文字表示也发生了变化,例如,PHP 文件的 mime 类型由“"PHP script text”变为“PHP script, ASCII text”。
Note:通常来说,使用 PHP 绑定的魔数数据库(设置$magic_file参数为空,不设置MAGIC环境变量)是最好的选择,除非你确实需要一个自定义的魔数数据库。
范例
面向对象风格
过程化风格
以上例程会输出:
text/plain; charset=us-ascii
参见
finfo_close()
关闭 fileinfo 资源
For most common image files:
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!
On my Debian squeeze system, the path needed is like :
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: Actually segfaults, where if I type the full name (including the file extension:) It works as expected, so I guess something goes wrong with "A .mime and/or .mgc suffix is added if needed."
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: 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.
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:
Couldn't get finfo to return the mimetype in the way expected so i made a function to do it with system() hope it works for other people too
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.
Notice FileInfo::__construct() has strange behavior in PHPOn 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 2. You can manually "fix" the mime-magic file by editing it and escaping all lines starting with !, thus changing each one to \!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)