HomeLanguagesPHP finfo_open() Function

PHP finfo_open() Function

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

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS