finfo_file() Function is one of the PHP operations used in filesystem and stream workflows. Detects MIME type with the procedural finfo_file() API. The examples below show what it does and how to handle its result.
What Is finfo_file() Function?
Detects MIME type with the procedural finfo_file() API.
Its syntax is:
finfo_file(finfo $finfo, string $filename, int $flags = FILEINFO_NONE, $context = null)
The parameters are:
finfo $finfo— A finfo object created using finfo_open() with the desired flags.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. You must create the finfo resource/object first and handle errors.
Important: You must create the finfo resource/object first and handle errors.
Example 1: Basic Usage
The basic example focuses on the behavior of finfo_file() Function: detects MIME type with the procedural finfo_file() API. Inputs are defined locally or shown as application-controlled paths.
<?php
$mimeInspector = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($mimeInspector, __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() Function. Notice the different checks, arguments, or output handling.
<?php
$mimeInspector = finfo_open(FILEINFO_MIME_TYPE);
if ($mimeInspector === false) { throw new RuntimeException("MIME detection unavailable"); }
$detectedType = finfo_file($mimeInspector, __FILE__);
var_dump($detectedType);
This variation matters because you must create the finfo resource/object first and handle errors.
Practical Example: Using the Operation in an Application
The following example demonstrates how finfo_file() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$inspector = finfo_open(FILEINFO_MIME_TYPE);
if ($inspector === false) { throw new RuntimeException("fileinfo unavailable"); }
$type = finfo_file($inspector, __DIR__ . "/asset.png");
if ($type === "image/png") { echo "PNG payload detected"; }
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 MIME type with the procedural finfo_file() API.
- Limitation: You must create the finfo resource/object first and handle errors.
Comparison With Related Functions
| Function or Method | Difference |
|---|---|
finfo_file() Function | Detects MIME type with the procedural finfo_file() API. |
finfo::file() | The object-oriented form of the same MIME inspection operation. |
Use finfo_file() Function 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
| Mistake | Correct Approach |
|---|---|
| Assuming this operation has the same behavior on every file or environment. | You must create the finfo resource/object first and handle errors. Check prerequisites and return values before using the result. |
| Treating finfo::file() as an interchangeable replacement for finfo_file() Function. | The object-oriented form of the same MIME inspection operation. |
Key Takeaways
- Detects MIME type with the procedural finfo_file() API.
- You must create the finfo resource/object first and handle errors.
- Related operation: finfo::file() — The object-oriented form of the same MIME inspection operation.
finfo_file() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.