Saturday, November 22, 2025
HomeLanguagesPHP finfo_file() Function

PHP finfo_file() Function

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

RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6783 POSTS0 COMMENTS
Nicole Veronica
11929 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS