PHP Classes

finfo: Determine the type of files analyzing its contents

Recommend this page to a friend!
  Info   View files Example   View files View files (17)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 348 All time: 7,008 This week: 310Up
Version License PHP version Categories
finfo 1.0GNU General Publi...5PHP 5, Files and Folders
Description 

Author

This class can determine the type of files analyzing its contents.

It provides an interface compatible with PHP finfo extension under systems that do not have that PHP extension available.

It takes the path of a file and uses the file command to analyze its contents when possible. Otherwise the class reads the file contents to guess the file type and character encoding.

Innovation Award
PHP Programming Innovation award nominee
March 2012
Number 6


Prize: One downloadable e-book of choice by O'Reilly
finfo is an useful PHP extension that makes it possible to determine the type of data contained in files by examining its contents.

Unfortunately the finfo extension is not available in all PHP setups. This class provides a work around by emulating the functionality of the finfo extensionin pure PHP code.

Manuel Lemos
Picture of Uku-Kaarel Jo~esaar
Name: Uku-Kaarel Jo~esaar <contact>
Classes: 8 packages by
Country: Estonia Estonia
Age: 41
All time rank: 3811 in Estonia Estonia
Week rank: 91 Up1 in Estonia Estonia Equal
Innovation award
Innovation award
Nominee: 5x

Example

<?php

require_once('./loader.finfo.php');

$files = scandir('./dir');

//$Yo = new myfinfo(FILEINFO_MIME_ENCODING);
//$Yo = new myfinfo(FILEINFO_MIME_TYPE);
//$Yo = new myfinfo(FILEINFO_MIME);
$Yo = new finfo(FILEINFO_MIME_ENCODING|FILEINFO_MIME_TYPE);
foreach(
$files as $file) {
   
//copy($file, './dir/'.end(explode('/',$file)));
   
echo '<b><a href="./dir/' . end(explode('/',rtrim($file,'/'))) .'">'. end(explode('/',rtrim($file,'/'))) .'</a></b> '. $Yo->file('./dir/'.$file) . "<br />\n";
   
//echo "<p> {$Yo->log} </p>\n";
}


Details

class.finfo.php --------------- public $no_magic <-- built in magic public $flags <-- finfo flags public $magic_file <-- path to magic file Extra methods are not implmented in php's own 'finfo' ..................................................... Set or overwrite/override magic file description set_magicDescription($key, $array); mime_class/sub_type get_mime_class() get_mime_subtype(); Returns true if mime class is "text", also returns true on XML, CSS, JS, RTF, PHP. isTextualData(); ---------------------------------------------------------------- About me: http://ukj.pri.ee ukj@ukj.pri.ee

  Files folder image Files  
File Role Description
Files folder imagedir (11 files)
Plain text file class.finfo.php Class methods to try guess file type
Accessible without login Plain text file example.multipleFiles.php Example Lists directory files and their mime types
Accessible without login Plain text file example.myMagicDef.php Aux. Shows how to add or replace magic pattern
Accessible without login Plain text file loader.finfo.php Aux. loads finfo class if needed
Accessible without login Plain text file readme.doc.txt Doc. About implementation specific features
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  dir  
File Role Description
  Accessible without login Image file A4_to_A5.png Data Auxiliary data
  Accessible without login Plain text file birds.rtf Data Auxiliary data
  Accessible without login Image file favicon.ico Data Auxiliary data
  Accessible without login Plain text file google_browser_detection.js Data Auxiliary data
  Accessible without login Plain text file HEADER Data Auxiliary data
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login Plain text file jquery.qtip.min.css Data Auxiliary data
  Accessible without login Image file sunset.jpg Icon Icon image
  Accessible without login Plain text file test.csv Data Auxiliary data
  Accessible without login Image file tree_expand.gif Icon Icon image
  Accessible without login Plain text file webgps.c.txt Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 94%
Total:348
This week:0
All time:7,008
This week:310Up