The PHP finfo::buffer API performs a focused operation on an object or value. The examples below show its effect on actual data.
Examines the contents of a byte string to estimate its MIME type.
What Is finfo::buffer in PHP?
Examines the contents of a byte string to estimate its MIME type.
Syntax:
finfo::buffer(string $string, int $flags = ..., $context = ...)
The method accepts the following parameters:
$string— Required. A string parameter named string; the examples below show a supported value.$flags— Optional. Bitmask options controlling iteration, caching, or property-access behavior.$context— Optional. A PHP parameter named context; the examples below show a supported value.
Return type declared by PHP: mixed / not explicitly declared. The value or side effect is demonstrated in the examples below.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$mimeDetector = new finfo(FILEINFO_MIME_TYPE);
echo $mimeDetector->buffer("plain text sample");
Output:
text/plain
Examines the contents of a byte string to estimate its MIME type.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$mimeDetector = new finfo(FILEINFO_MIME_TYPE);
echo $mimeDetector->buffer("hello from another document");
Output:
text/plain
MIME identification is not proof that an uploaded file is safe or valid; combine it with strict application rules.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$mimeDetector = new finfo(FILEINFO_MIME_TYPE);
echo $mimeDetector->buffer("one,two,three\n");
Output:
text/plain
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- MIME identification is not proof that an uploaded file is safe or valid; combine it with strict application rules.
- Examines the contents of a byte string to estimate its MIME type.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
finfo::buffer | Examines the contents of a byte string to estimate its MIME type. |
| mime_content_type() | finfo provides an object API and customizable flags; mime_content_type() is a simpler one-shot check. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of finfo::buffer | MIME identification is not proof that an uploaded file is safe or valid; combine it with strict application rules. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | finfo provides an object API and customizable flags; mime_content_type() is a simpler one-shot check. |
Key Takeaways
- Examines the contents of a byte string to estimate its MIME type.
- MIME identification is not proof that an uploaded file is safe or valid; combine it with strict application rules.
- Use the example that matches the values, runtime version and dependencies in your application.
finfo::buffer is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.