Open In App

PHP finfo_file() Function

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

The finfo_file() function is an inbuilt function in PHP that is used for getting information about a file.

Syntax:

  • Procedural Style:
    public finfo_file(
        string $filename, 
        int $flags = FILEINFO_NONE, 
        ?resource $context = null
    ): string|false
  • Object-Oriented Style:
    public finfo::file(
        string $filename, 
        int $flags = FILEINFO_NONE, 
        ?resource $context = null
    ): string|false

Parameters: This function accept four parameters that are described below:

  • finfo: It specifies an instance for finfo, which returns by finfo_open().
  • filename: It specifies the name of the file that we want to check.
  • flags:  This is file info constants.
  • context: By default, this null is assigned for a description of contexts.

Return Value: This function returns the textual description of the content of file, or false if an error occurred

Example 1: In the below example, we will print the file information using the finfo_file() function.

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                                                   

Example 2: In the below example, we will print the file information using finfo_file() functions.

PHP




<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); 
foreach (glob("*") as $filename) {
    echo finfo_file($finfo,$filename) . "\n";
}
finfo_close($finfo);
?>


Output:

text/x-php
application/x-empty

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads