Content · Programming · PHP

PHP finfo_buffer() Function: Usage and Examples

Learn how PHP finfo_buffer works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Detects a MIME type or other file metadata from in-memory bytes.

What Is finfo_buffer() in PHP?

Detects a MIME type or other file metadata from in-memory bytes.

Its syntax is:

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

Parameters:

  • $finfo — Accepted as finfo; required argument.
  • $string — Accepted as string; required argument.
  • $flags — Accepted as int; optional default FILEINFO_NONE.
  • $context — Accepted as ?resource; optional default null.

Return type: string|false. MIME detection is heuristic; never treat it alone as proof that uploaded content is safe.

Availability: Requires fileinfo extension.

Example 1: Basic Usage

<?php

$detector = new finfo(FILEINFO_MIME_TYPE); echo finfo_buffer($detector, "Hello");

Output:

text/plain

MIME detection is heuristic; never treat it alone as proof that uploaded content is safe.

Example 2: A Different Input

<?php

$detector = new finfo(FILEINFO_MIME_TYPE); echo finfo_buffer($detector, "%PDF-1.4\n");

Output:

application/pdf

MIME detection is heuristic; never treat it alone as proof that uploaded content is safe.

Practical Example: Applying It in a Program

<?php

$detector = new finfo(FILEINFO_MIME_TYPE); $uploadedBytes = "sample report"; $detectedMime = finfo_buffer($detector, $uploadedBytes); echo "Detected: " . $detectedMime;

Output:

Detected: text/plain

MIME detection is heuristic; never treat it alone as proof that uploaded content is safe.

Important Behavior and Edge Cases

  • Key behavior: MIME detection is heuristic; never treat it alone as proof that uploaded content is safe.
  • Environment: Requires fileinfo extension.

Comparison With Related Functions

FunctionPurpose
finfo_buffer()Detects a MIME type or other file metadata from in-memory bytes.
finfo_file()finfo_file() inspects a file by path or stream; finfo_buffer() inspects bytes already held in memory.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: MIME detection is heuristic; never treat it alone as proof that uploaded content is safeMIME detection is heuristic; never treat it alone as proof that uploaded content is safe.

Key Takeaways

  • Detects a MIME type or other file metadata from in-memory bytes.
  • MIME detection is heuristic; never treat it alone as proof that uploaded content is safe.
  • Requires fileinfo extension.

Summary: MIME detection is heuristic; never treat it alone as proof that uploaded content is safe.

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