PHP - FileInfo file() Function
The PHP FileInfo finfo_file() function is used to return information about a file. Also it can return a textual description of the contents of the filename argument, or false if an error occurred.
Syntax
Below is the syntax of the PHP FileInfo finfo_file() function −
string finfo_file ( resource $finfo , string $file_name [, int $options [, resource $context ]] )
Parameters
Below are the parameters of the finfo_file() function −
$finfo − It is the finfo instance, returned by finfo_open().
$file_name − It is the name of a file which is to be checked.
$options − It is used to specify one or more options and its default value is FILEINFO_NONE.
$context − It is used to specify a valid context resource created by stream_context_create().
Return Value
The finfo_file() function returns a string with the detected file type and MIME type and FALSE on failure.
PHP Version
First introduced in core PHP 5.3.0, the finfo_file() function continues to function easily in PHP 7, and PHP 8.
Example 1
Here is the basic example of the PHP FileInfo finfo_file() function to get the information about the file.
<?php
// Return MIME type ala mimetype extension
$finfo = finfo_open(FILEINFO_MIME);
foreach(glob("*") as $filename) {
echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);
?>
Output
Below is the outcome of the following code −
text/html; charset=us-ascii text/plain; charset=us-ascii text/csv; charset=us-ascii text/csv; charset=us-ascii application/zip; charset=binary text/plain; charset=us-ascii text/plain; charset=us-ascii
Example 2
Now in the below PHP code, the finfo_file() function accepts two arguments and returns the MIME type of the given file.
<?php
// Mention file address here
$file = '/PHP/PhpProjects/myfile.txt';
// Create a fileinfo resource
$finfo = finfo_open(FILEINFO_MIME_TYPE);
// Check if fileinfo resource is valid
if ($finfo !== false) {
// Get MIME type
$info = finfo_file($finfo, $file);
if ($info !== false) {
echo "MIME type: $info";
} else {
echo "Failed to retrieve MIME type.";
}
// Close fileinfo resource
finfo_close($finfo);
} else {
echo "Failed to open fileinfo.";
}
?>
Output
This will generate the below output −
MIME type: text/plain
Example 3
Now the below code retrieves file information of the remote file using finfo_file() and $context attribute.
<?php
// Mention file path here
$file = 'https://tutorialspoint.com/sample.pdf';
// Create a fileinfo resource
$finfo = finfo_open(FILEINFO_MIME_TYPE);
// Check if fileinfo resource is valid
if ($finfo !== false) {
// Get file content
$file_content = file_get_contents($file, false, $context);
// Get MIME type from file content
$info = finfo_buffer($finfo, $file_content);
if ($info !== false) {
echo "MIME type of remote file: $info";
} else {
echo "Failed to retrieve MIME type of remote file.";
}
// Close fileinfo resource
finfo_close($finfo);
} else {
echo "Failed to open fileinfo.";
}
?>
Output
This will create the below output −
MIME type of remote file: application/pdf
Example 4
In the following example, we are using the finfo_file() function to get the fetch the primary IP address with the number of redirects.
<?php
// Mention file path here
$file = 'nonexistent-file.txt';
// Check if file exists
if (file_exists($file)) {
// Create a fileinfo resource
$finfo = finfo_open(FILEINFO_MIME_TYPE);
// Check if fileinfo resource is valid
if ($finfo !== false) {
// Get MIME type
$info = finfo_file($finfo, $file);
if ($info !== false) {
echo "File type: $info";
} else {
echo "Failed to retrieve file information.";
}
// Close fileinfo resource
finfo_close($finfo);
} else {
echo "Failed to open fileinfo.";
}
} else {
echo "File '$file' does not exist.";
}
?>
Output
Following is the output of the above code −
File 'nonexistent-file.txt' does not exist.
Summary
The finfo_file() function is a built-in method in PHP which is used to retrieve the file information. And in this chapter we have seen four different examples to show the usage of the function for better grasp of it.