查詢

mime_content_type()函式—用法及示例

「 返回指定檔案的 MIME 型別 」


函式名:mime_content_type() 

適用版本:PHP 4 >= 4.3.0, PHP 5, PHP 7

函式說明:mime_content_type() 函式用於返回指定檔案的 MIME 型別。

語法:mime_content_type ( string $filename ) : string|false

引數:

  • $filename:指定的檔案路徑。

返回值:

  • 如果成功,則返回檔案的 MIME 型別。
  • 如果失敗,則返回 false。

示例:

// 示例1:返回圖片檔案的 MIME 型別
$filename = 'path/to/image.jpg';
$mime_type = mime_content_type($filename);
echo $mime_type; // 輸出:image/jpeg

// 示例2:返回文字檔案的 MIME 型別
$filename = 'path/to/text.txt';
$mime_type = mime_content_type($filename);
echo $mime_type; // 輸出:text/plain

// 示例3:返回不存在的檔案的 MIME 型別
$filename = 'path/to/nonexistent.file';
$mime_type = mime_content_type($filename);
var_dump($mime_type); // 輸出:bool(false)

注意事項:

  • 在某些作業系統上,需要安裝並配置 fileinfo 擴充套件才能正常使用該函式。
  • 如果無法獲取檔案的 MIME 型別,該函式可能會返回 "application/octet-stream",表示未知的二進位制檔案型別。
補充糾錯
上一個函式: min()函式
下一個函式: microtime()函式
熱門PHP函式
分享連結