Archive for the ‘php’ Category

Mats hosted a little competition last weekend. The main goal of the competition was to make a program which found the shortest path from . to X in a labyrinth, and the code size should be as small as possible.

My first approach was a depth-first seach algorithm, but when I ran the code on large labyrinths the algorithm took to long time. Then I tried a breadth-first search algorithm which proved much faster and stable on finding the shortest path.

Even if I did manage to get my code down to 518 bytes, I didn’t even get close to the winner, which got it down to 253 (results can be found here). You can take a look at my code here, it isn’t as advanced as the other examples shown on mats’ page but it works and finds the correct answer quickly.

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');