Function finfo_close is Deprecated in PHP 8.5

Function finfo_close is Deprecated in PHP 8.5

PHP provides the Fileinfo extension for detecting MIME types and file information. Before PHP 8.1, the finfo_close function was used to release a file info resource created by finfo_open. Since PHP 8.1, the finfo_open returns a finfo object rather than a traditional resource. These objects are automatically released when there are no remaining references or when explicitly destroyed.

Since PHP 8.5, the finfo_close function produces a deprecation warning:

<?php

$finfo = finfo_open(FILEINFO_MIME);
echo finfo_file($finfo, 'test.txt'); // text/plain; charset=us-ascii
finfo_close($finfo);

Example will output:

Deprecated: Function finfo_close() is deprecated since 8.5, as finfo objects are freed automatically in ...

In the previous example, we can resolve the deprecation warning by simply removing the finfo_close call. The finfo object will be cleaned up automatically once it goes out of scope. Alternatively, we can free it explicitly with unset($finfo) or by setting $finfo = null.

<?php

$finfo = finfo_open(FILEINFO_MIME);
echo finfo_file($finfo, 'test.txt'); // text/plain; charset=us-ascii
unset($finfo);

Leave a Comment

Cancel reply

Your email address will not be published.