查询

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函数
分享链接