Current Location: Home> Function Categories> mime_content_type

mime_content_type

Detect the MIME content type of the file
Name:mime_content_type
Category:File information Fileinfo
Programming Language:php
One-line Description:Returns the MIME type of the specified file

Function name: mime_content_type()

Applicable version: PHP 4 >= 4.3.0, PHP 5, PHP 7

Function Description: The mime_content_type() function is used to return the MIME type of the specified file.

Syntax: mime_content_type ( string $filename ) : string|false

parameter:

  • $filename: The specified file path.

Return value:

  • If successful, the MIME type of the file is returned.
  • If it fails, false is returned.

Example:

 // 示例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)

Notes:

  • On some operating systems, the fileinfo extension needs to be installed and configured to use the function properly.
  • If the MIME type of the file cannot be obtained, the function may return "application/octet-stream", indicating an unknown binary file type.
Similar Functions
Popular Articles