Content · Programming · PHP

PHP finfo::file() Method: Usage and Examples

Detects the MIME type of a file through a finfo object.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

finfo::file() Method is one of the PHP operations used in filesystem and stream workflows. Detects the MIME type of a file through a finfo object. The examples below show what it does and how to handle its result.

What Is finfo::file() Method?


Detects the MIME type of a file through a finfo object.

Its syntax is:

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

The parameters are:

  • string $filename — The source or destination filesystem path.
  • int $flags = FILEINFO_NONE — Option constants that alter how the operation behaves.
  • $context = null — Optional PHP stream context controlling relevant stream or wrapper behavior.

Return value: The declared return type is string|false. MIME inference is not a guarantee that arbitrary file content is safe.

Important: MIME inference is not a guarantee that arbitrary file content is safe.

Example 1: Basic Usage


The basic example focuses on the behavior of finfo::file() Method: detects the MIME type of a file through a finfo object. Inputs are defined locally or shown as application-controlled paths.

<?php

$mimeInspector = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $mimeInspector->file(__FILE__);
if ($mimeType !== false) { echo $mimeType; }

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for finfo::file() Method. Notice the different checks, arguments, or output handling.

<?php

$mimeInspector = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $mimeInspector->file(__DIR__ . "/sample.pdf");
if ($mimeType === "application/pdf") { echo "PDF detected"; }

This variation matters because mIME inference is not a guarantee that arbitrary file content is safe.

Practical Example: Using the Operation in an Application


The following example demonstrates how finfo::file() Method fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$mimeInspector = new finfo(FILEINFO_MIME_TYPE);
$path = __DIR__ . "/attachment.bin";
if (is_file($path) && $mimeInspector->file($path) === "application/pdf") { echo "Looks like a PDF"; }

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Detects the MIME type of a file through a finfo object.
  • Limitation: MIME inference is not a guarantee that arbitrary file content is safe.

Comparison With Related Functions


Function or MethodDifference
finfo::file() MethodDetects the MIME type of a file through a finfo object.
finfo_file()Uses the procedural API with an explicit finfo object; finfo::file() uses the object method.

Use finfo::file() Method for the specific behavior described above. The related operation, finfo_file(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming this operation has the same behavior on every file or environment.MIME inference is not a guarantee that arbitrary file content is safe. Check prerequisites and return values before using the result.
Treating finfo_file() as an interchangeable replacement for finfo::file() Method.Uses the procedural API with an explicit finfo object; finfo::file() uses the object method.

Key Takeaways


  • Detects the MIME type of a file through a finfo object.
  • MIME inference is not a guarantee that arbitrary file content is safe.
  • Related operation: finfo_file() — Uses the procedural API with an explicit finfo object; finfo::file() uses the object method.

finfo::file() Method is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.