XAMPP – PHP Fatal error: Class ‘ZipArchive’ not found

I just finished writing a program written in PHP today. When I moved to the another environment, I found that the script didn’t work. After some investigations, I found that the program stopped running because of the following error:

PHP Fatal error:  Class 'ZipArchive' not found in ...

The error is obvious, some PHP extensions were missing. Therefore, I ran the following to see which modules were being loaded by the php:

#Make sure that the php is living in /opt/lampp/
#which php
/opt/lampp/bin/php

#php -m
[PHP Modules]
...
...
xsl
zlib

Okay, the problem is clear, zip is missing.

For some (stupid) reasons, XAMPP (1.8.1) removes the zip support from the package. I don’t understand why because it was available in the older version (1.7.3). Removing a popular package will create troubles and backward compatibility issues. Anyway, the XAMPP is a non-professional product. It is not designed for production uses.

Before we go further, please make sure that your PHP is 5.4.7. It is because this tutorial is version specific.

#php -v

PHP 5.4.7

Here are the details:

#As always, we want to switch to root user first
sudo su

#Switch to the extension directory
cd /opt/lampp/lib/php/extensions/no-debug-non-zts-20100525

#Download the missing zip.so
#This zip.so was extracted from a 32-bit Linux environment, and was complied using PHP 5.4.7 32-bit i686
#It will not work with a different PHP version.
wget http://icesquare.com/download/zip.so

#Modify the permissions
chmod a+x zip.so
chown root:root zip.so

Now, we will need to modify the php.ini

#nano /opt/lampp/etc/php.ini

#Look for the zip.so and remove the comment

#From
;extension="zip.so"

#To
extension="zip.so"

Next, we will need to verify that the extension is being loaded by php:

#php -m
[PHP Modules]
...
...
xsl
zip < ------------ This one
zlib

If everything looks good, we will need to restart the apache.

#/opt/lampp/lampp reloadapache

That’s it! Now the error is gone. You can also verify it by including phpinfo() in your code. It should display the details about the zip package.

My last word: Stay away from XAMPP when possible. It is using 32-bit stone-age technology, full of security-related bugs (because they release one or two versions ever year), and is packaged in a very non-professional way (such as backward compatibility). Consider to move to native Apache + PHP + MySQL. Believe me, you will see at least 10% of the performance improvement.

–Derrick

Our sponsors:

PHP Network Error

One of my PHP web applications stopped working today. After I investigated the issues, I noticed that this is a very famous, yet unsolved error. I have no idea why it happens, but I do have a work around solution for it. Basically, this article applies to you if you match all of the following:

  • You are using PHP
  • Your web applications talk to other servers via domain name (e.g., example.com rather than 123.1.1.3)
  • You use XAMPP (instead of native Apache, PHP).

Notice that I am not 100% sure whether this has anything to do with XAMPP. But most of problem I experienced happen on XAMPP platform.

And here are some example problems:

  • Getting a file connect using file_get_contents(‘http://example.com/somepage.html’), or CURL etc.
  • Sending emails (SMTP server: ‘mail.example.com’)
  • Connect to a database server via domain name (‘example.com:3306’)

Why this problem happens?

This problem has nothing to do with your PHP code. In fact, the problem happens when PHP tries to look up the IP address of your domain name. Let’s take a look to the following example. Suppose I have the following code:

$data = file_get_contents('http://example.com/test.html')

//process the $data here...

When PHP executes this code, it will try to get the IP address of example.com first, and talk to the server to retrieve the content. This problem happens because PHP is unable to get the IP address of example.com.

Initially, I thought it was my server issues, therefore I tried to ping example.com on the server, i.e.,

#ping example.com

and the result looks fine to me. So the problem has nothing to do with the OS / server. Then I run the following code in PHP

$IP = gethostbyname('example.com');
echo $IP;

Normally, I expect to see the IP address of example.com. If it return ‘example.com’, that means PHP is unable to determine the IP address. That explains why the web application stops working.

Solution #1: Restart XAMPP

Try to restart the XAMPP to see whether it resolves the problem or not:

sudo /opt/lampp/lampp stopapache
sudo /opt/lampp/lampp startapache

This method aims to resolve the situation that the XAMPP was already started but the network was not available. Restarting the Apache server helps to resolve this problem.

Solution #2: /etc/hosts

Since PHP was unable to lookup the IP address, I decided to give some hints to PHP by editing /etc/hosts:

123.1.1.1   example.com
123.1.1.2   anotherexample.com

This is a quick and easy solution. However, if example.com is moved a different IP address, you will need to update the file. It is pain in long term.

Solution #3: Stop using XAMPP

As I mentioned earlier, I notice that this problem happen in XAMPP environment only. I haven’t experienced this kind of problem with native Apache and native PHP. So I guess it may have something to do with XAMPP.

In fact, it is quite easy to switch from XAMPP to native Apache+PHP+MySQL etc. Native applications give you better performance and reliability and most importantly: the packages get upgraded automatically.

Hope it helps.

–Derrick

Our sponsors:

XAMPP: Error 1! Couldn’t start Apache! … Sorry, I’ve no idea what’s going wrong.

Today I helped my friend to install XAMPP on a Debian system. When I tried to start the Apache webserver, I got the following message:

#/opt/lampp/lampp startapache
XAMPP: Starting Apache with SSL (and PHP5)...
XAMPP: Error 1! Couldn't start Apache!
XAMPP: Starting diagnose...
XAMPP: Sorry, I've no idea what's going wrong.
XAMPP: Please contact our forum http://www.apachefriends.org/f/

This problem is likely related to your Apache configuration file. To drill down to the root of the problem, we can take a look to the Apache error log:

tail /opt/lampp/log/error_log

and it should give you some info on how to solve this computer problem.

–Derrick

Our sponsors:

Installing XAMPP on Fedora 12 64-bit system

I was installing XAMPP on Fedora 11 64-bit today, and I saw this famous message:

sudo /opt/lampp/lampp startapache
XAMPP is currently only availably as 32 bit application. Please use a 32 bit compatibility library for your system.

The message is very clear that XAMPP doesn’t like 32-bit system. So, we will need to cheat XAMPP that the system is 32-bit. First, we will need to install some libraries:

sudo yum -y install glibc.i686 libgcc.i686 libstdc++.i686

and now, we need to skip the 32-bit annoying check. Use Nano or your favorite editor to open this file: /opt/lampp/lampp

sudo nano /opt/lampp/lampp

Replace the following from:

# XAMPP is currently 32 bit only
case `uname -m` in
       *_64)
       if /opt/lampp/bin/php -v > /dev/null 2>&1
       then
               :
       else
               $de && echo "XAMPP gibt es zur Zeit nur als 32-Bit Applikation. Bitte verwende eine 32-Bit Kompatibilitaetsbibliothek fuer Dein System."
               $de || echo "XAMPP is currently only availably as 32 bit application. Please use a 32 bit compatibility library for your system."
               exit
       fi
       ;;
esac

To:

# XAMPP is currently 32 bit only
#case `uname -m` in
#       *_64)
#       if /opt/lampp/bin/php -v > /dev/null 2>&1
#       then
#               :
#       else
#               $de && echo "XAMPP gibt es zur Zeit nur als 32-Bit Applikation. Bitte verwende eine 32-Bit Kompatibilitaetsbibliothek fuer Dein System."
#               $de || echo "XAMPP is currently only availably as 32 bit application. Please use a 32 bit compatibility library for your system."
#               exit
#       fi
#       ;;
#esac

Now run the following the start XAMPP:

sudo /opt/lampp/lampp startapache

Wow! Everything is working great!

–Derrick

Our sponsors: