I’m working on a project where the users can upload files, and I need to know the type of file they are uploading, a simple solution would be to check the extensions of the files, but this has many flaws, so I was looking for a reliable way to detect the type of file, and I found a PECL extension called file info this extension is enabled by default in PHP 5.3, unfortunately I’m running PHP 5.2.10, and the prod server also has this version.

So what I did to install this on ubuntu 9.10 karmic was the following:

root@mini:/etc/php5/apache2# apt-get install php5-dev libmagic-dev php-pear
....
root@mini:/etc/php5/apache2# pear channel-update pear.php.net
...
root@mini:/etc/php5/apache2# pecl install Fileinfo
....
Build process completed successfully
Installing '/usr/lib/php5/20060613+lfs/fileinfo.so'
install ok: channel://pear.php.net/Fileinfo-1.0.4

and finally I added the extension to php.ini

extension=fileinfo.so

I restarted the apache server, and tested with this

$fileInfo = new finfo(FILEINFO_MIME); 
$mimeType = $fileInfo->buffer(file_get_contents($uploadedFile));