Open In App

PHP finfo_set_flags() Function

Last Updated : 28 Apr, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The finfo_set_flags() function is an inbuilt function in PHP that is used to set or change the behavior of the Fileinfo extension. It allows you to modify the flags that control the file information returned by the Fileinfo functions.

Syntax:

finfo_set_flags(finfo $finfo, int $flags): bool

Parameters: This function accepts two parameters that are described below.

  • $finfo: A fileinfo resource obtained from finfo_open() function.
  • $flags: An integer representing the flags to be set. These flags are constants provided by PHP.

Return Values: The finfo_set_flags() function returns "true" if it successfully sets the flags otherwise this function returns "false".

Program 1: The following program demonstrates the finfo_set_flags() function.

PHP
<?php

// Create a new Fileinfo resource
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);

// Set the flags to include MIME encoding
// information
finfo_set_flags($fileInfo, 
    FILEINFO_MIME_TYPE | FILEINFO_MIME_ENCODING);

// Get the file type of a specific file
$filename = "./output.txt";
$fileType = finfo_file($fileInfo, $filename);
echo "File type of $filename is: $fileType\n";

// Close the Fileinfo resource
finfo_close($fileInfo);
?>

Output:

File type of ./output.txt is: text/plain; charset=us-ascii

Program 2: The following program demonstrates the finfo_file() function.

PHP
<?php

// Create a new Fileinfo resource to get MIME type
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);

// Check if the Fileinfo resource was
// created successfully
if (!$fileInfo) {
    die("Failed to create Fileinfo resource.");
}

// List of filenames to check
$filenames = ["example.php", "output.jpg"];

foreach ($filenames as $filename) {
    
    // Get the file type (MIME) of the current file
    $fileType = finfo_file($fileInfo, $filename);
    echo "File type of $filename is: $fileType\n";
}

// Close the Fileinfo resource
finfo_close($fileInfo);

?>

Output:

File type of example.php is: text/x-php
File type of output.jpg is: image/jpeg

Reference: https://www.php.net/manual/en/function.finfo-set-flags.php


Article Tags :

Similar Reads