Function finfo_file Now Works with Remote Streams in PHP 8.6

Function finfo_file Now Works with Remote Streams in PHP 8.6

In PHP, the finfo_file function returns information about a file, such as its MIME type. The function accepted local paths only. Passing a URL returned false, so determining the type of a remote file first required downloading it to a temporary file.

Since PHP 8.6, the finfo_file function works with remote streams:

<?php

$finfo = finfo_open(FILEINFO_MIME_TYPE);
var_dump(finfo_file($finfo, 'https://httpbin.org/json'));

Before PHP 8.6:

bool(false)

Since PHP 8.6:

string(16) "application/json"

The whole file is not downloaded. The function reads only the first chunk of the stream, which is enough to determine the type. Other stream wrappers work as well. For example, data://text/plain,test returns text/plain.

The allow_url_fopen directive has to be enabled. When it is disabled, the function returns false, the same as in earlier versions.

Leave a Comment

Cancel reply

Your email address will not be published.