PHP finfo_set_flags() Function
Last Updated :
22 Sep, 2023
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
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
finfo_set_flags( $fileInfo ,
FILEINFO_MIME_TYPE | FILEINFO_MIME_ENCODING);
$filename = "./output.txt" ;
$fileType = finfo_file( $fileInfo , $filename );
echo "File type of $filename is: $fileType\n" ;
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
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
if (! $fileInfo ) {
die ( "Failed to create Fileinfo resource." );
}
$filenames = [ "example.php" , "output.jpg" ];
foreach ( $filenames as $filename ) {
$fileType = finfo_file( $fileInfo , $filename );
echo "File type of $filename is: $fileType\n" ;
}
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
Share your thoughts in the comments
Please Login to comment...