- Fileinfo Functions
- Requirements
- Installation
- Runtime Configuration
- Resource Types
- Predefined Constants
- Code Examples / Notes » ref.fileinfo
- How to Enable PHP Fileinfo Extensions?
- How to enable PHP FileInfo Extension?
- How to enable PHP FileInfo extension in cPanel?
- How to enable PHP FileInfo extension XAMPP?
- Validate MIME types with PHP Fileinfo
- PHP - FileInfo Functions
- Predefined Constants
Fileinfo Functions
The functions in this module try to guess the content type and encoding of a file by looking for certain magic byte sequences at specific positions within the file. While this is not a bullet proof approach the heuristics used do a very good job.
Requirements
magic_open library is needed to build this extension.
Installation
Information for installing this PECL extension may be found in the manual chapter titled Installation of PECL extensions. Additional information such as new releases, downloads, source files, maintainer information, and a CHANGELOG, can be located here: � http://pecl.php.net/package/fileinfo
Runtime Configuration
This extension has no configuration directives defined in php.ini .
Resource Types
There is one resource used in Fileinfo extension: a magic database descriptor returned by finfo_open().
Predefined Constants
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
FILEINFO_NONE ( integer ) No special handling. FILEINFO_SYMLINK ( integer ) Follow symlinks. FILEINFO_MIME ( integer ) Return a mime string, instead of a textual description. FILEINFO_COMPRESS ( integer ) Decompress compressed files. FILEINFO_DEVICES ( integer ) Look at the contents of blocks or character special devices. FILEINFO_CONTINUE ( integer ) Return all matches, not just the first. FILEINFO_PRESERVE_ATIME ( integer ) If possible preserve the original access time. FILEINFO_RAW ( integer ) Don’t translate unprintable characters to a \ooo octal representation.
finfo_buffer — Return information about a string buffer finfo_close — Close fileinfo resource finfo_file — Return information about a file finfo_open — Create a new fileinfo resource finfo_set_flags — Set libmagic configuration options
Code Examples / Notes » ref.fileinfo
Well, it is hard to install and use this extension. There is better alternative - use lunux comand "file". For insturctions - "man file" from linux shell.
echo system("file -i -b file.pdf");
?>
application/pdf
How to Enable PHP Fileinfo Extensions?
The FileInfo PHP file is a small, open source library that provides a set of functions and objects to deal with file information. It is often used with WordPress CMS in order to manage various types of file types, such as audio, video, images, documents, and other file types.
FileInfo allows WordPress CMS to recognize many common file types, and it can help accurately identify the type of a file. This is useful for websites that often require images, audio, video, or other types of files to be uploaded.
The FileInfo library can be installed with the command line, the installer, and most PHP distributions, including those used by WordPress CMS.
How to enable PHP FileInfo Extension?
Below we will talk about PHP 8, but it can be applied to PHP 7.xx.
So, to enable the PHP Fileinfo extension on WordPress when using PHP 8, you will need to follow these steps:
1. Open your WordPress site’s wp-config.php file in a text editor.
2. Add the following line of code to the file, just above the line that says /* That’s all, stop editing! Happy publishing. */:
// Enable PHP Fileinfo extension if (PHP_MAJOR_VERSION >= 8)
3. Save the wp-config.php file and upload it to your web server.
4. Restart your web server for the changes to take effect.
After following these steps, the PHP Fileinfo extension should be enabled on your WordPress site when using PHP 8.
How to enable PHP FileInfo extension in cPanel?
To enable the PHP Fileinfo extension in cPanel, you will need to follow these steps:
- Log in to your cPanel account.
- In the “Software” section, click on the “Select PHP Version” icon.
- In the “PHP Extensions” section, select the “Fileinfo” extension from the list and click on the “Save” button.
- The Fileinfo extension should now be enabled on your server.
- If you have any PHP scripts that require the Fileinfo extension, you may need to restart your web server for the changes to take effect.
After following these steps, the PHP Fileinfo extension should be enabled on your server and available to your PHP scripts.
How to enable PHP FileInfo extension XAMPP?
To enable the PHP Fileinfo extension on XAMPP, you will need to follow these steps:
- Open the XAMPP Control Panel on your computer.
- Click on the “Config” button next to the Apache row, and then select the “PHP (php.ini)” option.
- In the PHP configuration file that opens, search for the following line:
After following these steps, the PHP Fileinfo extension should be enabled on your XAMPP installation and available to your PHP scripts.
Validate MIME types with PHP Fileinfo
How to check the file type in PHP and secure file uploads: it is important to validate MIME types in PHP. Especially of files uploaded through an upload form to your website. Using PHP, the best way to validate MIME types is with the PHP extension Fileinfo. Any other method might not be as good or secure as you might think…
PHP file input validation, The old^Wwrong way
It is quite common to only look at the file extension to determine what type of file it is (I was hoping to say “years and years ago”, but unfortunately I still see this on a regular basis…).
An image always has an extension like .jpg, .jpeg, .png or .gif, right? And a Portable Document Format is the only file type to use .pdf as extension. Oh we were wrong!
To validate a file’s extension, we used to use something in the line of:
if(eregi("\.jpg|\.jpeg|\.gif|\.png|\.bmp", $_FILES['userfile']['name']) != FALSE) < // do stuff // some more . >
Code language: PHP (php)
and whether this example uses ereg , eregi or preg_match doesn’t matter. Seeing an extension like jpg, jpeg, gif, png or bmp made sure these are only images, right? Not!
Some of us might expanded the validation of PHP uploaded files with an extra check:
PHP File Validation, A Better Way To Validate MIME Types , but only for images
A better way to validate MIME types in PHP is:
$imageInfo = getimagesize( $_FILES['userfile']['tmp_name'] ); if ($imageInfo['mime'] == ("image/png") || $imageInfo['mime'] == ("image/jpeg") || $imageInfo['mime'] == ("image/gif") || $imageInfo['mime'] == ("image/psd") || $imageInfo['mime'] == ("image/bmp"))
Code language: PHP (php)
This last check uses a PHP image function called getimagesize(). So this only works on images.
The getimagesize() function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondent HTTP content type.
But getimagesize() only works for images and fails on other file types. Therefore a lot of developers use (hopefully used to use?) mime_content_type() .
Well, let that function be deprecated in favor of the PECL extension Fileinfo . And on its turn, the PECL extension Fileinfo now is deprecated in favor of the PHP extension Fileinfo. Can you still follow?
So it is best to use the PHP extension Fileinfo, because it works for other file types than images too.
PHP Fileinfo extension: validate MIME types and secure file uploads in PHP
To properly validate MIME-types in PHP, in order to provide some sort of file upload security, you need to use PHP Fileinfo functions. This can validate Office MIME types and others.
An example PHP function to validate Office and PDF MIME types is:
/* * Follow me on Twitter: @Jan_Reilink, https://twitter.com/hertogjanr */ function check_doc_mime( $tmpname ) < // MIME types: http://filext.com/faq/office_mime_types.php $finfo = finfo_open( FILEINFO_MIME_TYPE ); $mtype = finfo_file( $finfo, $tmpname ); finfo_close( $finfo ); if( $mtype == ( "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) || $mtype == ( "application/vnd.ms-excel" ) || $mtype == ( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) || $mtype == ( "application/vnd.ms-powerpoint" ) || $mtype == ( "application/vnd.openxmlformats-officedocument.presentationml.presentation" ) || $mtype == ( "application/pdf" ) ) < return TRUE; > else < return FALSE; > > if( function_exists( "check_doc_mime" ) ) < if ( !check_doc_mime( $_FILES['userfile']['tmp_name'] ) ) < /* * Not a MIME type we want uploaded to our site, stop here * and return an error message, or just die(); */ > else < /* * a MIME type we support is uploaded. Continue with our * upload script */ > > /* Support https://www.paypal.me/jreilink , thank you! */ ?>
Code language: PHP (php)
This function can be used to verify the MIME type of files uploaded through HTTP $_POST . Proper user input validation is important for your website security!
PHP - FileInfo Functions
FileInfo functions module can try to guess a content type and encoding of a file by looking for certain magic byte sequences at a specific position within a file. While it's not a bulletproof approach, the heuristics used to do a very good job.
Before PHP 5.3.0 version, the magic_open library can be needed to build this extension.
This extension can be enabled by default as of PHP 5.3.0. Before this time, fileinfo was a PECL extension but no longer maintained there.
Windows users must include bundled php_fileinfo.dll DLL file in php.ini to enable this extension.
The libmagic library can be bundled with PHP but include PHP specific changes. A patch against libmagic named libmagic.patch is maintained and may be found within a PHP fileinfo extensions source.
Predefined Constants
The constants are defined below by this extension and can be available only when an extension has either compiled into PHP or dynamically loaded at runtime.
- FILEINFO_NONE (integer) − No special handling.
- FILEINFO_SYMLINK (integer) − Follow symlinks.
- FILEINFO_MIME_TYPE (integer) − Return the mime type. Available since PHP 5.3.0.
- FILEINFO_MIME_ENCODING (integer) − Return the mime encoding of the file. Available since PHP 5.3.0.
- FILEINFO_MIME (integer) − Return the mime type and mime encoding as defined by RFC 2045.
- FILEINFO_COMPRESS (integer) − Decompress compressed files. Disabled since PHP 5.3.0 due to thread safety issues.
- FILEINFO_DEVICES (integer) − Look at the contents of blocks or character special devices.
- FILEINFO_CONTINUE (integer) − Return all matches, not just the first.
- FILEINFO_PRESERVE_ATIME (integer) − If possible preserve the original access time.
- FILEINFO_RAW (integer) − Don't translate unprintable characters to a \ooo octal representation.
- FILEINFO_EXTENSION (integer) − Returns the file extension appropiate for a the MIME type detected in the file. For types that commonly have multiple file extensions, such as JPEG images, then the return value is multiple extensions speparated by a forward slash e.g.: "jpeg/jpg/jpe/jfif". For unknown types not available in the magic.mime database, then return value is ". ". Available since PHP 7.2.0.
This Function can return information about a string buffer.
This Function can close a fileinfo resource.
This Function can return information about a file.
This Function can create a new fileinfo resource.
This Function can set libmagic configuration option.
This Function can detect MIME Content-type for a file (deprecated).
This Function can set terminal attributes and baud rate for a serial port.
This Function is an alias of finfo_open().