在 Windows 安裝 File(1) command


0 Comments

因為 mime_content_type 好像有點 bug ,我花了一段時間 Google 後(雖然大多數的時間是在確認這是個 bug… orz)找到這個方法:安裝 File(1) command

  1. 這裡下載 File(1) command 的 BinariesDependencies
  2. 把 Binaries 中 /bin/ 資料夾中的東西全部放進 C:\WINDOWS\system32 (有些人的電腦可能是 C:\WINNT\system32)。
  3. 把 Dependencies 中 /bin/ 資料夾中的東西全部放進 C:\WINDOWS\system32 。
  4. 點兩下 file.exe 看看,如果提示找不到 pcre3.dll ,就把剛才從 Dependencies 解壓縮出來的 pcre.dll 改名為 pcre3.dll 。
  5. 把 Binaries 中的 /shared/ 資料夾放進 C:\WINDOWS ,連資料夾一起放。
  6. 到命令提示字元(MS-DOS)執行下面兩行:

    file -C -m C:\WINDOWS\share\file\magic
    file -C -m C:\WINDOWS\share\file\magic.mime

  7. 這樣就裝完了,你可以用 file -biN [filename] 測試看看,如果成功的話將會傳回該檔案的 MIME-TYPE 。

裝在上述路徑是因為那是系統預設的 PATH 。如果自己去加入新的 PATH ,裝在別的地方也是可以,第 7 步驟會成功,但接下來要從 PHP 呼叫時就會失敗了,可能有其他地方可以設定,但我懶得研究 XD

接下來 PHP 的部份就簡單了:

function get_mimetype($f) {
    return exec(trim('file -biN '.escapeshellarg($f)));
}

使用方法以下幾種都可以:

echo get_mimetype('example.rar');
echo get_mimetype('C:\www\example.rar');
echo get_mimetype('C:/www/example.rar');

Leave a Reply

Your comment may not display immediately due to spam filtering. Please wait for moderation.