The finfo_file Function

The finfo_file function determines the MIME type of file contents. Its first parameter accepts a resource returned by the finfo_open function, and the second - the path to the file. The function returns a string with the MIME type or false on error.

Syntax

phpfinfo_file(
  resource $finfo,
  string $filename,
  int $options = FILEINFO_NONE,
  resource $context = null
): string|false

Example

Let's determine the MIME type of the file 'image.png':

php<?php
  $finfo = finfo_open(FILEINFO_MIME_TYPE);
  $res = finfo_file($finfo, 'image.png');
  finfo_close($finfo);
  echo $res;
?>

Code execution result:

php'image/png'

Example

Let's try to determine the type of a non-existent file:

php<?php
  $finfo = finfo_open(FILEINFO_MIME_TYPE);
  $res = finfo_file($finfo, 'nonexistent.file');
  finfo_close($finfo);
  var_dump($res);
?>

Code execution result:

phpfalse

Example

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

php<?php
  $finfo = finfo_open(FILEINFO_MIME_TYPE);
  $res = finfo_file($finfo, 'document.txt');
  finfo_close($finfo);
  echo $res;
?>

Code execution result:

php'text/plain'

See Also