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
finfo_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
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$res = finfo_file($finfo, 'image.png');
finfo_close($finfo);
echo $res;
?>
Code execution result:
'image/png'
Example
Let's try to determine the type of a non-existent file:
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$res = finfo_file($finfo, 'nonexistent.file');
finfo_close($finfo);
var_dump($res);
?>
Code execution result:
false
Example
Let's determine the MIME type of a text file:
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$res = finfo_file($finfo, 'document.txt');
finfo_close($finfo);
echo $res;
?>
Code execution result:
'text/plain'
See Also
-
the
mime_content_type
function,
which determines the MIME type -
the
file_exists
function,
which checks file existence -
the
is_file
function,
which checks a file