fileinfo for PHP on Leopard

Currently I had to use the fileinfo-Package on my development-system which is a MacBook Pro running MacOS 10.5 with Marc Lianages PHP-Package.

But sadly that does not contain the fileinfo-extension.

No Problem I thought: Installing a PECL-Extension isn’t hard work.

At least if it works….

Of course I ran into the problem that I couldn’t compile the fileinfo extension due to the following error-message

configure: error: Please reinstall the libmagic distribution
ERROR: `/private/tmp/pear/download/Fileinfo-1.0.4/configure' failed

I can’t be the only one that ran into the problem, I thought. And so it was.

While searching the internet, I generally found two ways to solve the problem.

  1. Use PHP 5.3, that already contains the package
  2. Install a complete second OperatingSystem beneath a perfectly working MacOS. Here for instance I found a way to get the installation working by installing the complete fink-package.

Using PHP 5.3 was no option for me, as we are developing for PHP5.2 and installing a second Operating System seemed somewhat oversized for the simple task of installing a small PHP-extension.

Back to the roots

So what was the original problem again? A missing installation of the libmagic-library. Event though the info-binary is installed on the MacOS I’m running, a shared libmagic-Library is missing.

So what the hell, why not compile one?

  • So I got myself the current binaries from unpacked the source and pulled out the Terminal.

    cd /path/to/unpacked/source  
    ./configure --prefix=/usr/local/libmagic 
    sudo make install 

    That left me with a running shared library of libmagic in the folder /usr/local/libmagic/lib

  • Second I used the already loaded source of the fileinfo-extension that caused the error due to the missing libmagic-library.

    So I opened /private/tmp/pear/download/Fileinfo-1.0.4/configure with my favourite Text-Editor (currently it is TextWrangler) and went to somewhere around line 4212 and changed it from

    SEARCH_PATH="/usr/local /usr /usr/share/file"


    SEARCH_PATH="/usr/local /usr /usr/share/file /usr/local/libmagic"

    and again took my Terminal to go to the fileinfo-directory and finally compile the fileinfo-extension

    cd /private/tmp/pear/download/Fileinfo-1.0.4/
  • Because of the apache2 on my MacBook Pro that somehow thinks it is running on a 64bit Machine, even though it definitely is a 32bit one – would be nice though – I had to adapt the build-process somewhat by compiling a universal-binary so that the extension will be loaded by the apache.

    Some info on that can be found here and here at Apples Developer pages

    So I ran the following command in Terminal

    make CFLAGS="-arch i386 -arch ppc64 -arch x86_64" LDFLAGS="-arch ppc -arch i386 -arch ppc64 -arch x86_64"
    sudo make install

    Aternatively you can export the CFLAGS and LDFLAGS variable by running these commands in Terminal:

    export FLAGS="-arch i386 -arch ppc64 -arch x86_64"
    export LDFLAGS="-arch ppc -arch i386 -arch ppc64 -arch x86_64"

    Then you can simply run

    make install

    That’s been the hard bit.

  • Last Steps

    Now the only things missing are the integration into php and the restart of the WebServer.

    For integrating the extension into php I added a file to the php-Directory via

    vi /usr/local/php5/php.d/50-extension-fileinfo

    where I simply added the line

  • And finaly the restart of the WebServer with

    sudo apachectl restart (or graceful)

Now I can call a phpinfo-file and there should be a line stating that the fileinfoextension is enabled.

One thought on “fileinfo for PHP on Leopard

Comments are closed.