class FileFormatIdentifier {         // File format table     // Format: $mime => array(name => $formatName, ext => array($extensions...))     protected $formatTable = array(         'image/jpeg' => array('name' => 'JPEG/JIFF Image', 'ext' => array('jpg', 'jpeg', 'jpe')),         'image/png' => array('name' => 'Portable (Public) Network Graphic', 'ext' => array('png')),         'video/mng' => array('name' => 'Multi-image Network Graphic Animation', 'ext' => array('mng')),         'image/gif' => array('name' => 'Graphic Interchange Format', 'ext' => array('gif')),         'image/tga' => array('name' => 'Truevision Targa Graphic', 'ext' => array('tga')),         'image/tif' => array('name' => 'Tagged Image Format File', 'ext' => array('tif')),         'image/bmp' => array('name' => 'Windows OS/2 Bitmap Graphic', 'ext' => array('bmp')),         'image/photoshop' => array('name' => 'Photoshop Format Image', 'ext' => array('psd')),         'application/msword' => array('name' => 'Word Document', 'ext' => array('doc')),         'application/msexcel' => array('name' => 'Excel Worksheet', 'ext' => array('xls')),         'video/avi' => array('name' => 'Audio Video Interleave File', 'ext' => array('avi')),         'audio/wav' => array('name' => 'Waveform Audio', 'ext' => array('wav')),         'audio/mid' => array('name' => 'MIDI-sequention Sound', 'ext' => array('mid', 'midi')),         'audio/mpeg' => array('name' => 'MPEG Audio Stream, Layer III', 'ext' => array('mp3')),         'video/mpeg' => array('name' => 'MPEG 1 System Stream', 'ext' => array('mpg', 'mpeg')),         'video/quicktime' => array('name' => 'QuickTime Video Clip', 'ext' => array('mov')),         'application/x-shockwave-flash' => array('name' => 'Macromedia Flash Format File', 'ext' => array('swf')),         'application/pdf' => array('name' => 'Acrobat Portable Document Format', 'ext' => array('pdf')),         'application/x-zip-compressed' => array('name' => 'Compressed Archive File', 'ext' => array('zip')),         'application/x-rar-compressed' => array('name' => 'WinRAR Compressed Archive', 'ext' => array('rar', 'r01')),         'application/x-ace-compressed' => array('name' => 'WinAce Compressed File', 'ext' => array('ace')),         'application/x-7z-compressed' => array('name' => '7-Zip Compressed File', 'ext' => array('7z')),         'application/x-bzip' => array('name' => 'Bzip 2 UNIX Compressed File', 'ext' => array('bz2', 'tbz2', 'tb2')),         'application/x-gzip' => array('name' => 'Gzip Compressed Archive', 'ext' => array('gz')),         'application/x-tar' => array('name' => 'Tape Archive File', 'ext' => array('tar')),         'font/ttf' => array('name' => 'TrueType Font', 'ext' => array('ttf')),         'font/otf' => array('name' => 'Open Type Font Format', 'ext' => array('otf')), // No idea about mime type         'text/html' => array('name' => 'HyperText Markup Language', 'ext' => array('htm', 'html')),         'text/xml' => array('name' => 'Extensible Markup Language File', 'ext' => array('xml'))     );     // Ident reference table     // Format: array($representation, $ident, $mime)     // 's' => string, 'h' => hexadecimal     protected $identRefTable = array(         array('h', 'd0cf11e0a1b11ae100', 'application/msword'),         #array('h', 'd0cf11e0a1b11ae100', 'application/msexcel'),         array('h', '89504e470d0a1a0a00', 'image/png'),         array('h', '8a4d4e470d0a1a0a00', 'video/mng'),         array('h', '0001000000', 'font/ttf'),         array('h', '4f54544f00', 'font/otf'),         array('h', 'ffd8ff', 'image/jpeg'),         array('h', '4944330', 'audio/mpeg'),         array('h', '000001', 'video/mpeg'),         array('s', '8BPS', 'image/photoshop'),         array('s', 'MThd', 'audio/mid'),         array('s', '**ACE**', 'application/x-ace-compressed'),         array('s', 'Rar!', 'application/x-rar-compressed'),         array('s', 'PK', 'application/x-zip-compressed'),         array('s', 'BZh', 'application/x-bzip'),         array('h', '1f8b08', 'application/x-gzip'),         array('s', '7z', 'application/x-7z-compressed'),         #array('s', 'ustar', 'application/x-tar'), // should be at byte position 257         #array('s', 'RIFF', 'audio/wav'),         array('s', 'RIFF', 'video/avi'),         array('s', 'GIF8', 'image/gif'),         array('s', 'MM.*', 'image/tif'),         array('s', 'II*', 'image/tif'),         array('h', '424d', 'image/bmp'),         array('s', '%PDF', 'application/pdf'),         array('s', 'FWS', 'application/x-shockwave-flash'),         array('h', '6d', 'video/quicktime'),         array('h', '00', 'image/tga'),         array('h', 'ff', 'audio/mp3'),         array('s', 'identRefTable as &$ident) {             $len = strlen($ident[1]);             if ($len > $maxLength) $maxLength = $len;         }         $this->maxLength = $maxLength; //      foreach ($this->identRefTable as $ident => $mime) //          echo "$mime: $ident => ".pack('A', $ident)."
";     }       public function infoFor($mime) {         if (!isset($this->formatTable[$mime])) return null;         return $this->formatTable[$mime];     }       public function identify($target, $returnType = 'mime') {                 // Make sure the target is a file we can work with         if (!is_file($target)) throw new Exception("'$target' is not a valid file.");           // Attempt to read the file header (the first $this->maxLength bytes) from the target file         $bin = file_get_contents($target, false, null, 0, 50);         if ($bin === false) throw new Exception("Failed to get contents of '$target'.");           // Convert the header to a hexadecimal representation to work with         $hex = bin2hex($bin);                 // Return the requested info         foreach ($this->identRefTable as $ident) {             if ($ident[0] == 's') $cmp = &$bin;             else $cmp = &$hex;             if (substr($cmp, 0, strlen($ident[1])) == $ident[1])                 switch ($returnType) {                     case 'format':                     case 'name':                         return $this->formatTable[$ident[2]]['name']; break;                     case 'ext': return $this->formatTable[$ident[2]]['ext'][0]; break;                     case 'exts': return $this->formatTable[$ident[2]]['ext']; break;                     case '*': return $this->formatTable[$ident[2]]; break;                     case 'mime':                     default:                          return $ident[2];                 }         }                 // No match found in table         return '';       }   }