Skip to content

Stdlib: ext/fileinfo — finfo_open/finfo_file/mime detection (ext/fileinfo parity) #3366

Closed

Description

@PurHur

Category

stdlib

Problem

MIME/type detection via finfo_open(), finfo_file(), finfo_buffer(), and finfo_close() is missing. Upload validators and deploy tooling use fileinfo to sniff content types without trusting extensions.

Verified Jun 2026:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php -r "var_export(function_exists(\"finfo_open\")); echo \"\n\";"
php -r "var_export(function_exists(\"finfo_open\")); echo \"\n\";"
'
# VM: false
# Zend (with ext/fileinfo): true

php-src reference

Repro — finfo_file (phase 1)

<?php
$f = finfo_open(FILEINFO_MIME_TYPE);
if ($f === false) {
    fwrite(STDERR, "finfo_open failed\n");
    exit(1);
}
echo finfo_file($f, __FILE__), "\n";
finfo_close($f);
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
php repro.php   # Zend: text/x-php or text/plain
Runtime Result
Zend MIME string for .php file
VM today Call to undefined function finfo_open()

Repro — finfo_buffer (phase 1b)

<?php
$f = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_buffer($f, "<?php echo 1;"), "\n";
finfo_close($f);

Constants (register in phase 1)

From php-src ext/fileinfo/fileinfo.stub.php — at minimum:
FILEINFO_NONE, FILEINFO_MIME_TYPE, FILEINFO_MIME, FILEINFO_MIME_ENCODING, FILEINFO_RAW

Scope (this repo)

Module Path
Extension ext/fileinfo/ (new module) or ext/standard/finfo.php delegating to system libmagic
Build document libmagic pkg-config dep; optional NO_FILEINFO for minimal self-host builds
VM resource/object handle wrapping magic_t
JIT/AOT VM-only v1; document deferral in capability matrix
Tests test/compliance/cases/stdlib/finfo_mime.phpt, finfo_buffer.phpt
Matrix script/capability-matrix.php

Prefer thin C ABI (libmagic calls only) over reimplementing magic rules in PHP.

Done when

  • finfo_file() repro MIME matches Zend on same host for local .php file
  • finfo_buffer() returns MIME for in-memory PHP snippet
  • FILEINFO_MIME_TYPE constant defined
  • Invalid path → false + warning like Zend
  • ./script/ci-fast.sh --filter finfo green (or @requires libmagic)
  • Capability matrix documents VM-only if JIT deferred

Related

Activity

  1. added
    enhancementNew feature or request
    phase-4:stdlibPhase 4 – stdlib for web apps
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on May 29, 2026
  2. PurHur commented on Jul 15, 2026

    @PurHur
    OwnerAuthor

    claim: PHP Compiler STD Lib Advanced — batch (issues #3366 finfo; follow-ups SessionHandler + PharData #6490 / socket_create_pair #6563 after merge)

    Implementing ext/fileinfo (finfo_open/file/buffer/close/set_flags + finfo class + FILEINFO_* constants) PHP-in-PHP via VmMime sniff; VM-first, no new C runtime.

  3. PurHur commented on Jul 15, 2026

    @PurHur
    OwnerAuthor

    Merged via #19239

    Verification

    php bin/vm.php test/repro/maintainer_gap_finfo_mime.php
    # text/x-php / text/x-php / bad_false / text/plain; charset=us-ascii / ok
    
    vendor/bin/phpunit --filter finfo_mime test/compliance/VMTest.php
    # OK (1 test)
    
    ./script/check-generated-docs.sh
    # all generated docs in sync
    

    PHP-in-PHP ext/fileinfo via VmMime sniff; VM-only (JIT deferred). Next batch items: #6490 PharData, #6563 socket_create_pair.

  4. PurHur commented on Jul 15, 2026

    @PurHur
    OwnerAuthor

    Batch progress: #3366 merged (PR #19239); #6490 PharData merged (PR #19245). Filed follow-ups for SessionHandler + finfo FILEINFO_NONE depth; #6563 socket_create_pair handed off (needs socket_write/read).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions