Open In App

PHP finfo_file() Function

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

Syntax:



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

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
$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
$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


Article Tags :