My server is finally up and I started writing a little web script to unpack and move files from a local place on disk to a ftp folder. I decided to use php since it is a language i know and it is quite efficient on small programs.

I bumped into some problems installing the fileinfo resource extension when using a windows server. I used the php msi installer to install php and used the same installer to add the fileinfo extension, but the installer didn’t include all the files. You also need some extra files in the php/extras folder, these files can be found on sorcefourge. You need the magic and magic.mgc files located in the bin package.

When the extension was successfully installed I tried with a simple example to see if it would match the filetype.

$fi = new finfo(FILEINFO_MIME, 'C:\PHP\extras\magic');
echo 'filetype ' . $fi->file('e:\test.rar');

this code should print “filetype application/x-rar”, but it showed application/x-dpkg instead. The reason was that windows returns a match if the condition starts with “!”, so i had to add “\” in front of every condition that started with “!” in order to get a correct match.

example:

0    string        !<arch>\ndebian application/x-dpkg
should be:
0    string        \!<arch>\ndebian application/x-dpkg

If you don’t like to use objects you could use this code instead:

$fi = finfo_open(FILEINFO_MIME, 'C:\PHP\extras\magic');
echo 'filetype: ' . finfo_file($fi, 'e:\test.rar');

Leave a Reply