GithubHelp home page GithubHelp logo

filedetector's Introduction

PHP CLASS FILEDETECTOR

Description

This class can detect the filetype by its content (header and trailer). A file can match several file definitions. Zip is a container used in zip archives, office documents, software packages and other. Then you get the results in order with the highest rankings on top.

The base for the decision are the signatures from https://www.garykessler.net/software/index.html#filesigs

Examples

Initialization:

require './src/filedetector.class.php';
$oFD=new axelhahn\filedetector();

Example: reference an existing local file

// set a file to analyze
$oFD->setFile($sMyfile); 

With the dump method you get some detected internal details:

// show extension, header and trailer bytes of the file
echo $oFD->dump(); 
Filename    : build/FileSigs_20200424/FTK_sigs_GCK.zip
Extension   : ZIP
First bytes : 50 4B 03 04 0A 00 00 00 00 00 3A 77 98 50 00 00 00 00 00 00
Last bytes  : 05 06 00 00 00 00 99 04 99 04 BF DA 01 00 53 44 05 00 00 00

The method getType returns the array of all file types matching first and last bytes.

// get all matching filetypes
print_r($oFD->getType());

It returns the score in the first subkey (here: "6") containing an array with details about filetype and detection values.

Array                                                                     
(                                                                       
    [6] => Array                                                        
        (                                                               
            [0] => Array                                                
                (                                                       
                    [description] => PKZIP archive_1                    
                    [extension] => ZIP                                  
                    [fileclass] => Compressed archive                   
                    [offset] => 0                                       
                    [header] => 50 4B 03 04                             
                    [trailer] => 50 4B ????????????????? 00 00 00       
                    [_scoring] => Array                                 
                        (                                               
                            [signature] => 5                            
                            [extension] => 1                            
                            [trailer] => 0                              
                        )                                               
                                                                        
                )                                                       
                                                                        
            [1] => Array                                                
                (                                                       
                    [description] => MacOS X Dashboard Widget           
                    [extension] => ZIP                                  
                    [fileclass] => MacOS                                
                    [offset] => 0                                       
                    [header] => 50 4B 03 04                             
                    [trailer] => (null)                                 
                    [_scoring] => Array                                 
                        (                                               
                            [signature] => 5                            
                            [extension] => 1                            
                            [trailer] => 0                              
                        )                                               
                                                                        
                )                                                       
                                                                        
        )                                                               
                                                                        
    [5] => Array                                                        
        (                                                               
            [0] => Array                                                
                (                                                       
                    [description] => Android package                    
					(...)

To return just the best match you can use the parameter true

// get the best matching filetype only
print_r($oFD->getType(true));
Array
(
    [description] => PKZIP archive_1
    [extension] => ZIP
    [fileclass] => Compressed archive
    [offset] => 0
    [header] => 50 4B 03 04
    [trailer] => 50 4B ????????????????? 00 00 00
    [_scoring] => Array
        (
            [signature] => 5
            [extension] => 1
            [trailer] => 0
        )

)

Example: you can try to detect a file type by a few first bytes as hexadecimal values - with or without spaces:

$oFD->setSignature('11 22 33 44 66 74 79 70 69 73 6F 6D');
echo $oFD->dump();
Filename    :                                    
Extension   :                                    
First bytes : 11 22 33 44 66 74 79 70 69 73 6F 6D
Last bytes  :                                    
print_r($oFD->getType());
Array                                                         
(                                                                   ```
    [5] => Array                                                    
        (                                                           
            [0] => Array                                            
                (                                                   
                    [description] => ISO Base Media file (MPEG-4) v1
                    [extension] => MP4                              
                    [fileclass] => Multimedia                       
                    [offset] => 4                                   
                    [header] => 66 74 79 70 69 73 6F 6D             
                    [trailer] => (null)                             
                    [_scoring] => Array                             
                        (                                           
                            [signature] => 5                        
                            [extension] => 0                        
                            [trailer] => 0                          
                        )                                           
                                                                    
                )                                                   
                                                                    
        )                                                           
                                                                    
)

filedetector's People

Contributors

axelhahn avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.