PHP - FileInfo finfo class



The PHP finfo class is used to provide an object-oriented interface into fileinfo functions. So it helps o get information about files

Syntax

Below is the syntax of the PHP FileInfo finfo class −

finfo {
   public __construct([ int $options = FILEINFO_NONE [, string $magic_file = "" ]] )
   public buffer( string $string [, int $options = FILEINFO_NONE [, resource $context ]] ) : string
   public file( string $file_name [, int $options = FILEINFO_NONE [, resource $context ]] ) : string
   public set_flags( int $options ) : bool
}

Return Value

The finfo class returns different values for each method −

  • __construct() − It return value is none.

  • buffer() − It returns string which is the type of the data or false on failure.

  • file() − It returns string which is the type of the file or false on failure.

  • set_flags() − It returns true on success and false on failure.

PHP Version

First introduced in core PHP 5.3.0, the finfo class continues to function easily in PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP FileInfo finfo class to get information about files or strings.

<?php
   /// Create a new finfo object
   $finfo = new finfo(FILEINFO_MIME_TYPE);
   
   // Analyze a file
   $fileType = $finfo->file('/PHP/PhpProjects/myfile.txt');
   echo "File type: $fileType\n";
   
   // Analyze a string
   $stringType = $finfo->buffer('Hello, world!');
   echo "String type: $stringType";
   
   // Set new flags
   $finfo->set_flags(FILEINFO_NONE);
?>

Output

Here is the outcome of the following code −

File type: text/plain
String type: text/plain

Example 2

In the below PHP code we will try to use the finfo class to get the MIME type of a file.

<?php
   // Create a new finfo object 
   $finfo = new finfo(FILEINFO_MIME_TYPE);
   
   // Get the MIME type of a file
   $fileType = $finfo->file('/PHP/PhpProjects/document.pdf');
   echo "File type: $fileType"; 
?> 

Output

This will generate the below output −

File type: application/pdf

Example 3

Now the below code to get the MIME encoding of a file using finfo class after creating a new finfo object with the MIME encoding flag.

<?php
   // Create a new finfo object
   $finfo = new finfo(FILEINFO_MIME_ENCODING);
   
   // Get the MIME encoding of a file
   $fileEncoding = $finfo->file('/PHP/PhpProjects/example.txt');
   echo "File encoding: $fileEncoding"; 
?> 

Output

This will create the below output −

File encoding: us-ascii

Example 4

In the following example, we are using the finfo function to analyze a string MIME type.

<?php
   // Create a new finfo object
   $finfo = new finfo(FILEINFO_MIME_TYPE);
   
   // Analyze a string
   $string = "Hello, world!";
   $stringType = $finfo->buffer($string);
   echo "String type: $stringType"; 
?> 

Output

Following is the output of the above code −

String type: text/plain

Summary

The above examples show different ways to use the finfo class to check file types, file encodings, and string types.

php_function_reference.htm
Advertisements