PHP - FileInfo construct Function



The PHP FileInfo construct() function is the constructor method for creating a FileInfo object. So it initializes the object and prepares it to use. This method does not return a value.

But you can get important details from the FileInfo object by calling its methods, like file().

Syntax

Below is the syntax of the PHP FileInfo construct() function −

public finfo::__construct([ int $options = FILEINFO_NONE [, string $magic_file = NULL ]] )

Parameters

Below are the parameters of the construct() function −

  • $options − It sets various options for the FileInfo object. Its by default value is FILEINFO_NONE.

  • $magic_file − It is the path to a custom magic file.

Return Value

The construct() function does not return a value.

PHP Version

First introduced in core PHP 5.3.0, the construct() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP FileInfo construct() function to create FileInfo object for getting the MIME type of a file.

<?php
   // Create a new FileInfo object
   $finfo = new finfo(FILEINFO_MIME_TYPE);
   
   // Check the MIME type of a file
   $file = '/PHP/PhpProjects/myfile.txt';
   $fileType = $finfo->file($file);
   
   echo 'The MIME type of ' . $file . ' is: ' . $fileType;
?>

Output

Here is the outcome of the following code −

The MIME type of /PHP/PhpProjects/myfile.txt is: text/plain

Example 2

Now we will use the construct() function and create FileInfo object to get the MIME encoding of a file.

<?php
   // Create a new FileInfo object 
   $finfo = new finfo(FILEINFO_MIME_ENCODING);
   
   // Check the MIME encoding of a file
   $file = 'example.txt';
   $fileEncoding = $finfo->file($file);
   
   echo 'The MIME encoding of ' . $file . ' is: ' . $fileEncoding;
?> 

Output

This will generate the below output −

The MIME encoding of /PHP/PhpProjects/myfile.txt is: us-ascii

Example 3

Now the below code creates a new FileInfo object with the help of the custom magic file.

<?php
   // Path to a custom magic file
   $magicFile = '/path/to/custom/magic/file';
   
   // Create a new FileInfo object 
   $finfo = new finfo(FILEINFO_NONE, $magicFile);
   
   // Check the type of a file 
   $file = '/PHP/PhpProjects/myfile.txt';
   $fileInfo = $finfo->file($file);
   
   echo 'File info for ' . $file . ' using custom magic file: ' . $fileInfo;
?> 

Output

This will create the below output −

File info for /PHP/PhpProjects/myfile.txt using custom magic file: ASCII text

Example 4

In the following example, we are using the construct() function to create a FileInfo object and get the file type.

<?php
   // Create a new FileInfo object
   $finfo = new finfo();
   
   // Check the file type of a file
   $file = '/PHP/PhpProjects/sample.txt';
   $fileInfo = $finfo->file($file);
   
   echo 'File info for ' . $file . ' is: ' . $fileInfo;   
?> 

Output

Following is the output of the above code −

File info for /PHP/PhpProjects/sample.txt is: ASCII text

Summary

The above examples show the various ways that various file related information can be retrieved using the __construct function of the FileInfo class.

php_function_reference.htm
Advertisements