2024 年 PHP 日本研討會

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

物件導向風格 (建構子)

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

這個函式會開啟一個魔術資料庫並返回其執行個體。

參數

flags

一個或多個 Fileinfo 常數 的位元或運算結果。

magic_database

魔術資料庫檔案的名稱,通常類似 /path/to/magic.mime。如果未指定,則會使用 MAGIC 環境變數。如果未設定環境變數,則會使用 PHP 內建的魔術資料庫。

傳遞 null 或空字串將等同於預設值。

返回值

(僅限程序式風格)成功時返回一個 finfo 執行個體,失敗時返回 false

更新日誌

版本 說明
8.1.0 現在返回一個 finfo 執行個體;先前返回的是 資源
8.0.3 magic_database 現在可以為 null。

範例

範例 #1 物件導向風格

<?php
$finfo
= new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // 返回 MIME 類型,例如 mimetype 副檔名

/* 取得特定檔案的 MIME 類型 */
$filename = "/usr/local/something.txt";
echo
$finfo->file($filename);

?>

範例 #2 程序式風格

<?php
$finfo
= finfo_open(FILEINFO_MIME, "/usr/share/misc/magic"); // 返回 MIME 類型,例如 mimetype 副檔名

if (!$finfo) {
echo
"開啟 fileinfo 資料庫失敗";
exit();
}

/* 取得特定檔案的 MIME 類型 */
$filename = "/usr/local/something.txt";
echo
finfo_file($finfo, $filename);

/* 關閉連線 */
finfo_close($finfo);
?>

上述範例將輸出

text/plain; charset=us-ascii

注意事項

注意:

一般來說,使用內建的魔術資料庫(透過不設定 magic_databaseMAGIC 環境變數)是最佳的做法,除非您特別需要自訂的魔術資料庫。

參見

新增筆記

使用者貢獻的筆記 9 則筆記

illusivefingers at gmail dot com
12 年前
我正在使用 Windows 7 和 Apache。我花了幾個小時才找出它無法運作的原因。

首先,在 php.ini 中啟用 php_fileinfo.dll 擴充套件。您還需要四個位於以下連結庫中的 magic 檔案

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

必須設定一個環境變數或直接指向名為「magic」的檔案的路徑,且不帶任何副檔名。

然後,確保 xdebug 已關閉,或將 ini 的 error_reporting 設定為不顯示腳本的注意事項或警告。

希望這可以為其他人省下幾個小時的挫折感!
dario dot borreguero at gmail dot com
16 年前
平台:WinXP-SP2、PHP5.2.5、MySQL 5.0、Apache 2.2.8

閱讀之前的筆記後,我仍然無法載入我的 magic 資料庫:「magic.mime」檔案(從 GnuWin32 專案下載,與 v4.21 二進位檔案一起壓縮)。我總是得到一個無效的 $finfo 物件,或者 finfo_open(...) 返回 FALSE。

為了能夠載入「magic.mime」檔案,Fileinfo 函式庫(包含在 PHP5.2.5 中)也需要「magic」檔案。

例如
1. 資料庫
c:\php\magic.mime
c:\php\magic

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

更多資訊請參閱:http://pecl.php.net/bugs/bug.php?id=7555

請注意 'christophe dot charron dot xul at gmail dot com' 新增的註釋

注意:在升級到 PHP5.2.5 之前,我使用的是 PHP5.2.1,它只需要「magic.mime」檔案。
匿名
11 年前
對於大多數常見的影像檔案
<?php
函數 minimime($fname) {
$fh=fopen($fname,'rb');
如果 (
$fh) {
$bytes6=fread($fh,6);
fclose($fh);
如果 (
$bytes6===false) 返回 false;
如果 (
substr($bytes6,0,3)=="\xff\xd8\xff") 返回 'image/jpeg';
如果 (
$bytes6=="\x89PNG\x0d\x0a") 返回 'image/png';
如果 (
$bytes6=="GIF87a" || $bytes6=="GIF89a") 返回 'image/gif';
返回
'application/octet-stream';
}
返回
false;
}
?>
olivier dot berger at it-sudparis dot eu
13 年前
在我的 Debian squeeze 系統上,需要的路徑類似
<?php
$finfo
= new finfo(FILEINFO_MIME, "/usr/share/misc/magic.mgc");
?>
php at brudaswen dot de
16 年前
由於我花了一些時間才找到 Windows 需要的 magic 資料庫檔案,這裡提供一個小提示

目前包含兩個所需檔案 (magic 和 magic.mime) 的 GnuWin32 專案的最新版本是 "file-4.23"。
4.23 之後到 4.25-1 的所有版本都不包含這兩個必需的檔案。

希望這個資訊有幫助。
php at kingsquare dot nl
16 年前
目前的版本 (1.04) 不支援與伺服器預設值不同的 mime.magic 資料庫。

(因此文件內容不正確)
Ubuntu 的預設位置是 '/usr/share/file/magic'。為了讓範例正常運作,所有 finfo_open() 指令都必須使用額外的對應位置
<?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
17 年前
無法讓 finfo 以預期的方式返回 MIME 類型,所以我製作了一個使用 system() 的函數來執行此操作

<?php
函數 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;
}
?>

希望這也能幫助其他人
mark at dynom dot nl
16 年前
似乎在不同的發行版和 magic 檔案的載入上存在相當多的不一致性。

在 Archlinux 上,檔案位於此處
/usr/share/misc/file/magic.mgc

但是這個

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

實際上會造成區段錯誤,而如果我輸入完整名稱(包含副檔名):

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

它就能如預期般運作,所以我猜「如有需要,會加上 .mime 和/或 .mgc 後綴」這部分出了點問題。
tularis at php dot net
17 年前
在 Windows 系統上,使用者可能會發現這總是返回「application/x-dpkg」。
有兩種方法可以解決這個問題
1. 從 GnuWin32 的 <http://sourceforge.net/projects/gnuwin32/> 取得 mime-magic 資料庫檔案
2. 您可以透過編輯 mime-magic 檔案並將所有以 ! 開頭的行加上跳脫字元,將每個 ! 改為 \! 來手動「修復」該檔案
To Top