PHP - FileInfo mime_content_type() Function



The PHP FileInfo mime_content_type() function is used to detect MIME Content-type for a file. This function can return the MIME content type for a file as determined by using information from the magic.mime file. It can return the content type in MIME format, like text/plain or application/octet-stream.

Syntax

Below is the syntax of the PHP FileInfo mime_content_type() function −

string mime_content_type ( string $filename )

Parameters

This function accepts $filename parameter which is a file path to the tested file.

Return Value

The mime_content_type() function returns a string that represents the MIME type of the file and FALSE on failure.

PHP Version

First introduced in core PHP 4.3.0, the mime_content_type() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP FileInfo mime_content_type() function to find the MIME content type of the given file.

<?php
   // Mention filename here
   $filename1 = "/PHP/PhpProjects/myfile.txt";
   echo mime_content_type($filename1) . "\n";
?>

Output

Here is the outcome of the following code −

text/plain

Example 2

In the below PHP code we will use the mime_content_type() function and get the MIME content type of the provided file.

<?php
   // Mention filename here
   $filename2 = "/PHP/PhpProjects/test.php";
   echo mime_content_type($filename2);
?> 

Output

This will generate the below output −

text/x-php

Example 3

Now the below code retrieves specific information from mime_content_type(), and prints it.

<?php
   $filename3 = '/PHP/PhpProjects/document.pdf';
   echo mime_content_type($pdf); 
?> 

Output

This will create the below output −

application/pdf

Example 4

In the following example, we are using the mime_content_type() function to get the content type of the file.

<?php
   $filename4 = '/PHP/PhpProjects/song.mp3';
   echo mime_content_type($audio);  
?> 

Output

Following is the output of the above code −

audio/mpeg
php_function_reference.htm
Advertisements