• PHP Video Tutorials

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.

Sr.No Function & Description


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().