php8.5
Home/ Manual/ fileinfo / functions/ finfo_file

finfo_file

PHP function Edit on GitHub ✎

(PHP >= 5.3.0, PHP 7, PHP 8, PECL fileinfo >= 0.1.0)

Return information about a file

Description

Procedural

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

Oop

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

This function is used to get information about a file.

Parameters

finfo

Finfo

filename

Name of a file to be checked.

flags

One or disjunction of more Fileinfo constants.

context

For a description of contexts, refer to ref.stream.

Return Values

Returns a textual description of the contents of the filename argument, or false if an error occurred.

Errors/Exceptions

Throws a ValueError if filename contains any null bytes. Prior to PHP 8.5.0, a TypeError was thrown instead.

Changelog

VersionDescription
8.5.0A ValueError is now thrown instead of a TypeError when filename contains null bytes.
8.0.0context is nullable now.

Examples

A finfo_file() example

php
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type aka mimetype extension
foreach (glob("*") as $filename) {
    echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);
?>

The above example will output something similar to:

output
text/html
image/gif
application/vnd.ms-excel

See Also

Source: reference/fileinfo/functions/finfo-file.xml · from the official PHP manual (php/doc-en)