November 21, 2008

PHP-Pecl FileInfo Package Installation Ubuntu

To get a file's mime type within a php code, you can use mime_content_type(), but it is deprecated. So instead of it you can use Pecl/FileInfo package. In order to install FileInfo package to your Ubuntu system, you need to install these packages first (i assume you already have apache2, php5, libapache2-mod-php5 etc);

build-essential (this gets rid of => C compiler cannot create executables)
php5-dev (this gets rid of => sh: phpize: command not found)
php5-pear
libmagic-dev (this gets rid of =>
checking for magic files in default path... not found
configure: error: Please reinstall the libmagic distribution
ERROR: `/tmp/pear/cache/Fileinfo-1.0.4/configure' failed)

libmagic1

Then you do as root;
pear channel-update pear.php.net
pecl install Fileinfo


Then within your php.ini files (/etc/php5/apache2/php.ini and /etc/php5/cli/php.ini), you add this line;
extension=fileinfo.so

And lastly as root we will copy these files;
cp /usr/share/file/magic.mime /etc/magic
cp /usr/share/file/magic.mime /etc/magic.mime


Start the apache server and you are ready to go;
/etc/init.d/apache2 restart

And here is the php code to get file's mime type;
$finfo = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($finfo, $file_path);
finfo_close($finfo);

No comments: