finfo_open / finfo_file / mime_content_type

finfo_open(int $flags = FILEINFO_NONE, string $magic_database = ''): finfo|false · finfo_file(finfo $finfo, string $filename, int $flags = FILEINFO_NONE): string|false · mime_content_type(string|resource $filename): string|false

MIME type detection: finfo_open builds the detector, finfo_file classifies by header magic bytes (filename-independent), mime_content_type is the one-step shorthand; backed by the libmagic database, unknown magic yields a generic default instead of an error.

C C23

C23
#include <stdio.h>
#include <string.h>

/* no MIME database in libc - hand-rolled magic check (libmagic does this for PHP finfo) */
static const unsigned char PNG_MAGIC[8] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};

int main(void) {
    unsigned char png[16] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
                             0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52};
    printf("%d\n", memcmp(png, PNG_MAGIC, 8) == 0);
    printf("%d\n", memcmp("just text", PNG_MAGIC, 8) == 0);
    return 0;
}
Output
1
0

C++ C++26

C++26
#include <cstring>
#include <iostream>
#include <vector>

// no MIME database in the C++ standard library - hand-rolled magic check
// (libmagic does this for PHP finfo)
static const unsigned char PNG_MAGIC[8] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};

int main() {
    std::vector<unsigned char> png = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
                                      0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52};
    std::cout << std::boolalpha
              << (std::memcmp(png.data(), PNG_MAGIC, 8) == 0) << "\n"
              << (std::memcmp("just text", PNG_MAGIC, 8) == 0) << "\n";
    return 0;
}
Output
true
false

C# v11.0.100

v11.0.100
using System;
using System.IO;

// .NET has no magic-number API (third-party MimeDetective in real projects):
// hand-rolled header check - what finfo's database does for PHP
byte[] png = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D,
               0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
               0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89 };
bool isPng = png.Length >= 8 && png[0] == 0x89 && png[1] == 0x50 && png[2] == 0x4E && png[3] == 0x47;
Console.WriteLine(isPng);
byte[] text = { 0x6a, 0x75, 0x73, 0x74, 0x20, 0x74, 0x65, 0x78, 0x74 }; // "just text"
Console.WriteLine(text.Length >= 8 && text[0] == 0x89 && text[1] == 0x50);
Output
True
False

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"net/http"
)

// net/http.DetectContentType: stdlib magic-number sniffing (like finfo's database)
func main() {
	png := []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01\x00\x00\x00\x01" +
		"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\x00IEND\xae\x42\x60\x82")
	fmt.Println(http.DetectContentType(png)) // image/png - from bytes, not names
	fmt.Println(http.DetectContentType([]byte("just text"))) // unknown -> generic default
}
Output
image/png
text/plain; charset=utf-8

Java v27.0.1

v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws Exception {
        // no magic-byte API: probeContentType is name-based on most JDKs;
        // the byte check is hand-rolled (finfo's magic database does this for PHP)
        byte[] png = {(byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
        boolean isPng = png[0] == (byte) 0x89 && png[1] == 'P' && png[2] == 'N' && png[3] == 'G';
        System.out.println(isPng); // true: PNG signature from bytes
        Path p = Files.createTempFile("fin_demo", null); // no extension on purpose
        Path named = p.resolveSibling("photo.png");
        Files.move(p, named);
        System.out.println(Files.probeContentType(named)); // image/png: from the NAME
        Files.deleteIfExists(named);
    }
}
Output
true
image/png

JavaScript v26.13.1

v26.13.1
// Node has no magic-number API: hand-rolled header check (finfo's database does this for PHP)
const png = Buffer.from("89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000049454e44ae426082", "hex");
const PNG_MAGIC = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
console.log(png.subarray(0, 8).equals(PNG_MAGIC)); // true: PNG signature from bytes
console.log(Buffer.from("just text").subarray(0, 8).equals(PNG_MAGIC)); // false: unknown magic
Output
true
false

PHP v8.6.0

v8.6.0
// PNG magic bytes decide the type - not the filename (this temp file has NO extension)
$png = "\x89PNG\r\n\x1a\n"
    . "\x00\x00\x00\x0dIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89"
    . "\x00\x00\x00\x00IEND\xae\x42\x60\x82";
$file = tempnam(sys_get_temp_dir(), "fin_");
file_put_contents($file, $png);
$f = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($f, $file), "\n"; // image/png: magic-number detection from bytes
finfo_close($f);
echo mime_content_type($file), "\n"; // shorthand: same magic database, one call
file_put_contents($file, "just text"); // unknown magic -> generic default, not an error
$f = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($f, $file), "\n";
finfo_close($f);
unlink($file);
Output
image/png
image/png
text/plain

Python v3.15.2

v3.15.2
import mimetypes
import os
import tempfile

# PHP finfo sniffs MAGIC BYTES; Python stdlib only maps FILENAME -> type (weaker!)
PNG = (b"\x89PNG\r\n\x1a\n\x00\x00\x00\x0dIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
       b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\x00IEND\xae\x42\x60\x82")
p = tempfile.mktemp(prefix="fin_")  # no extension on purpose
with open(p, "wb") as f:
    f.write(PNG)
# no byte-level API in stdlib: hand-rolled magic check (finfo's database does this for PHP)
print(PNG.startswith(b"\x89PNG\r\n\x1a\n"))  # True: PNG signature
print(mimetypes.guess_type(p))                  # (None, None): no extension -> no answer
os.remove(p)
print(mimetypes.guess_type("photo.png"))        # ('image/png', None): from the NAME only
Output
True
(None, None)
('image/png', None)

Rust v1.99.1

v1.99.1
use std::fs;

// std has no MIME database (the infer crate in real projects) - hand-rolled magic check
const PNG_MAGIC: [u8; 8] = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];

fn main() {
    let png: Vec<u8> = vec![0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
                            0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52];
    let p = std::env::temp_dir().join("fin_demo_noext"); // no extension on purpose
    fs::write(&p, &png).unwrap();
    let data = fs::read(&p).unwrap();
    println!("{}", data.starts_with(&PNG_MAGIC)); // true: from bytes, not names
    fs::remove_file(&p).unwrap();
    println!("{}", b"just text".starts_with(&PNG_MAGIC)); // false: unknown magic
}
Output
true
false

TypeScript v26.13.1

v26.13.1
// Node has no magic-number API: hand-rolled header check (finfo's database does this for PHP)
const png: Buffer = Buffer.from("89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000049454e44ae426082", "hex");
const PNG_MAGIC: Buffer = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
console.log(png.subarray(0, 8).equals(PNG_MAGIC)); // true: PNG signature from bytes
const text: Buffer = Buffer.from("just text");
console.log(text.subarray(0, 8).equals(PNG_MAGIC)); // false: unknown magic
Output
true
false

Language differences

Magic-number detection vs extensions: finfo/libmagic sniffs header bytes (PNG signature 89 50 4E 47 0D 0A 1A 0A) and ignores the filename — the example's temp file has no extension yet still yields image/png; Python's mimetypes.guess_type and Java's Files.probeContentType (on most JDKs) map by filename, so a missing extension gives (None, None)/null — the byte-based vs name-based difference is exactly this entry's teaching point.

PHP surface: finfo_open(FILEINFO_MIME_TYPE) creates a finfo resource (the fileinfo extension, since 5.3, backed by libmagic), finfo_file probes a file, finfo_close releases it; mime_content_type is the same library's one-step shorthand (no resource handle); the OO form new finfo() works too. Unknown magic returns a generic default (text/plain) rather than an error.

Echoing the existing getimagesize entry: getimagesize hand-parses the PNG IHDR chunk for dimensions (the size face), while this entry uses libmagic's magic database for MIME (the type face); all ten language examples assert against the same fixed PNG header bytes.

Magic-number surfaces per language: Go's net/http.DetectContentType sniffs in the stdlib (the HTTP spec's sniffing table); everyone else follows the honest strategy — Python (python-magic third-party), Java (Apache Tika), C# (MimeDetective), JS/TS (the file-type package), Rust (the infer crate), C/C++ (link libmagic directly) — so the examples hand-roll the 8-byte PNG signature check (memcmp/startsWith/equals).

Same lineage as the file(1) command line: the description file xxx.png prints comes from the very same libmagic database (finfo's magic_database parameter can point at a custom signature file).