The mime_content_type Function

The mime_content_type function returns the MIME type of a file by analyzing its contents. The file path is passed as a parameter. This function is useful when working with file uploads to the server when you need to verify their type.

Syntax

phpmime_content_type(string $filename);

Example

Let's determine the MIME type for an image:

php<?php
  $res = mime_content_type('image.jpg');
  echo $res;
?>

Code execution result:

php'image/jpeg'

Example

Let's determine the MIME type for a text file:

php<?php
  $res = mime_content_type('document.txt');
  echo $res;
?>

Code execution result:

php'text/plain'

Example

Let's check the MIME type of a PDF document:

php<?php
  $res = mime_content_type('file.pdf');
  echo $res;
?>

Code execution result:

php'application/pdf'

See Also

  • the finfo_file function,
    which determines the file type
  • the pathinfo function,
    which returns information about a path
  • the is_file function,
    which checks a file