Content · Programming · PHP

PHP finfo_open() Function: Usage and Examples

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

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

Creates a fileinfo object used to identify MIME types and file formats.

What Is finfo_open() in PHP?

Creates a fileinfo object used to identify MIME types and file formats.

Its syntax is:

finfo_open(int $flags = FILEINFO_NONE, ?string $magic_database = null): finfo|false

Parameters:

  • $flags — Accepted as int; optional default FILEINFO_NONE.
  • $magic_database — Accepted as ?string; optional default null.

Return type: finfo|false. Always check for initialization failure. Detection is an aid to validation, not a security guarantee.

Availability: Requires fileinfo extension.

Example 1: Basic Usage

<?php

$detector = finfo_open(FILEINFO_MIME_TYPE); echo get_debug_type($detector);

Output:

finfo

Always check for initialization failure. Detection is an aid to validation, not a security guarantee.

Example 2: A Different Input

<?php

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

Output:

text/plain

Always check for initialization failure. Detection is an aid to validation, not a security guarantee.

Practical Example: Applying It in a Program

<?php

$detector = finfo_open(FILEINFO_MIME_TYPE); $payload = "sample text"; $mimeType = finfo_buffer($detector, $payload); echo $mimeType;

Output:

text/plain

Always check for initialization failure. Detection is an aid to validation, not a security guarantee.

Important Behavior and Edge Cases

  • Key behavior: Always check for initialization failure. Detection is an aid to validation, not a security guarantee.
  • Environment: Requires fileinfo extension.

Comparison With Related Functions

FunctionPurpose
finfo_open()Creates a fileinfo object used to identify MIME types and file formats.
new finfo()new finfo() offers an object-oriented detector; finfo_open() returns a procedural detector handle.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Always check for initialization failureAlways check for initialization failure. Detection is an aid to validation, not a security guarantee.

Key Takeaways

  • Creates a fileinfo object used to identify MIME types and file formats.
  • Always check for initialization failure. Detection is an aid to validation, not a security guarantee.
  • Requires fileinfo extension.

Summary: Always check for initialization failure. Detection is an aid to validation, not a security guarantee.

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