Open In App

PHP finfo_open() Function

Last Updated : 23 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The finfo_open() function is an inbuilt function in PHP that creates a new info instance. This function is in PHP 7 and 8.

Syntax:

  • Procedural Style:
finfo_open(Flag, magic_database = null)
  • Object-Oriented Style:
public finfo::__construct(Flag, $magic_database = null)

Parameters: This function has only two parameters.

  • Flag: One or disjunction of more file constants.
  • magic-database: It specifies the name of a magic database like this /path/magic. The Magic environmental variable is used, in case if not specified. If the environmental variable is not set then PHP’s bundled magic database will be used. Passing a null or an empty string will be equivalent to the default value. 

Return Value: On successful, it will return finfo instance, otherwise, it will return false on failure.

Example 1: In the below example, we will use Object Oriented Style.

PHP




<?php
$finfoinstance = new finfo(FILEINFO_MIME,null); 
  
// Return file MIME
$filename = "./text.txt";
echo $finfoinstance->file($filename);
?>


Output:

application/x-empty; charset=binary

Example 2:  In the below example, we will use Procedural Style.

PHP




<?php
$finfoinstance = finfo_open(FILEINFO_MIME,null); 
  
if(!($finfoinstance)){
    echo "Opening file data is failed" ;
    exit() ;
}
  
// Return file MIME
$filename = "./text.txt";
echo finfo_file($finfoinstance,$filename);
finfo_close($finfoinstance) ;
  
?>


Output:

application/x-empty; charset=binary

Reference:  https://www.php.net/manual/en/function.finfo-open.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads