PHP Classes
elePHPant
Icontem

PHP Detect File Type: Identify files via MIME type and file signatures

Recommend this page to a friend!
  Info   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-03-04 (23 days ago) RSS 2.0 feedStarStarStarStar 73%Total: 255 This week: 1All time: 7,433 This week: 693Up
Version License PHP version Categories
file-identifier 1.0GNU General Publi...5.4PHP 5, Files and Folders
Description Author

This class can identify files via MIME type and file signatures.

It can take the path of a given file and read the beginning of its contents to detect the type of file based on recognizing signatures of well known file types.

The class can also detect the associated MIME type using the PHP finfo extension.

  Performance   Level  
Name: Martin Latter is available for providing paid consulting. Contact Martin Latter .
Classes: 5 packages by
Country: United Kingdom United Kingdom
Age: ???
All time rank: 211994 in United Kingdom United Kingdom
Week rank: 95 Up4 in United Kingdom United Kingdom Up
Innovation award
Innovation award
Nominee: 3x

Details

File Identifier

Identify a file through MIME type and file signature detection.

Purpose

Detect a file's type through MIME type information and file signature (header) bytes.

(File extensions are ignored.)

Uses

*nix systems have the file command, which is a powerful file identifier, and callable from PHP's shell functions.

Nevertheless, this command is not always available.

The main uses of File Identifier are:

  • For \nix servers where PHP shell functions are disabled inphp.ini* ( disable_functions= )
  • PHP running on Windows, where no file command natively exists
  • When older versions of file (e.g. v. 5.09) are installed on the \*nix server, and which do not recognise some files such as .gpg
  • Where custom, old, or rare file types cannot be identified by the file command.

Example Usage

require('classes/fileidentifier.class.php');
require('classes/filesignatures.class.php');

use CopySense\FileIdentifier\FileIdentifier;

$f = new FileIdentifier('mira.png');
$r = $f->getResult();
echo $r['mimeinfo'] . PHP_EOL . $r['fileinfo'];

File Signatures

The file signature data (classes/filesignatures.class.php) contain a limited range of common file type signatures in hexadecimal bytes. This data array can be easily extended with additional and custom file signatures.

License

File Identifier is released under the GPL v.3.

  Files folder image Files  
File Role Description
Files folder imageclasses (2 files)
Files folder imagefile_examples (1 file)
Accessible without login Plain text file cmdline_example.php Example Example script
Accessible without login Plain text file general_example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  classes  
File Role Description
  Plain text file fileidentifier.class.php Class Class source
  Plain text file filesignatures.class.php Class Class source

  Files folder image Files  /  file_examples  
File Role Description
  Accessible without login Image file mira.png Icon Icon image

 Version Control Unique User Downloads Download Rankings  
 100%
Total:255
This week:1
All time:7,433
This week:693Up
User Ratings User Comments (1)
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:73%StarStarStarStar
Rank:181