Archive

Posts Tagged ‘pecl’

File Type Detection in PHP

December 24th, 2009 Ivan Villareal No comments

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 runing 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

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

 

Categories: Linux, PHP Tags: , , ,