After some series of “political” technical notes, I think it’s the right moment to supply some more technical posts to fellow developers. This time I will write about creating sizzling upload progress bar in PHP. In the past, there had been heated debate in PHP internal list about RFC 1867 implementation on PHP core code. If you were there, you might still remember that we had to patch the file main/rfc1867.c and some other files and then recompile PHP to get the upload progress bar hack working. Some harsh critics even mentioned that without the built-in capability of upload progress, PHP was years behind Java and Perl and lacked its capability as a web programming language.
Fortunately, since PHP 5.2 (especially after PHP 5.2.6 release), showing upload progress is not a big deal in PHP. Thanks to APC developers -mostly are also core PHP developers- who contributed to changes in APC that led to the availability of this long-waited feature. With APC we’ll be able to track the progress of file upload and provide our users better convenience when using our application.
Still, APC is not a part of core PHP shipped as a bundled package. We need to install it manually. In Windows, we can simply load the dll file to get it working. However, Linux users may need some pointers about how to install and enable this package. Hence, I will provide some guide for APC installation which was tested on veteran RHEL 4 and energetic young Fedora 10. So, let’s just go to the installation part.
Basic knowledge of Linux, access to root shell, yum installed on the machine
1. Installing APC package with yum.
a. Lucky Luke installation
In this type of installation, we will try to install APC as php-apc package. If you are successful to run the following two commands, you can consider yourself lucky and simply proceed to step 2.
root#yum update php
root#yum install php-apc</strong>
After running the last command, you may realize how the path of your installation was destined. If you see this message on the console “No package php-apc available.” you were destined to do the type b installation instead.
b. Donald Duck installation
We have to do some more extra effort just like how Donald has to struggle against his misfortune. However, this type of installation offers more benefit for you when you decide to install more packages from PECL in the future. Different with previous installation in which we try to download php-apc package from the OS repository, in this installation, we will use pecl, a helper tool from PHP which is used to download and compile community extensions not shipped with default bundled package.
Please type the following commands on the console subsequently.
<strong> root#yum update php
root#yum install php-pear
root#yum install php-devel
root#yum install httpd-devel
root#pecl install apc</strong>
Note: when you’re asked to install APC with Apache apxs, just say yes.
2. Verifying the compiled apc.so module is located in the same directory with other PHP modules.
In some cases, the shared library apc.so created from step 2 was put in another directory which is different with location of the rest of php modules. You need to verify that this library is indeed integrated with its peers in the same directory. An example of verification is provided as the following:
<strong> root#locate php/modules</strong>
->For example, results show that the modules are located in /usr/libr64/php/modules/*
<strong> root#locate apc.so</strong>
->For example, results show that the module is located in /usr/lib64/20060613/apc.so
//Move apc.so to the directory where other php modules reside
<strong> root#cp -f /usr/lib64/20060613/apc.so /usr/libr64/php/modules/</strong>
3. Creating apc.ini entry or updating php.ini
We can create a new ini file called apc.ini and then put apc related configurations in this file. We can also simply append entry to php.ini. The commands below show the creation and updating of apc.ini
<strong>root#echo "extension=apc.so" > /etc/php.d/apc.ini
When we are modifying apc.ini, verify that it contains minimum configuration like below
;Load APC extension
4. Restarting Httpd daemon
<strong> root#/etc/init.d/httpd restart</strong>
5.Testing if everything is working as expected
Run php command (line) and check if APC is now part of it.
<strong>root#php -i |grep apc</strong>
<strong> root#php -r "phpinfo();" |grep apc</strong>
Congratulations. You have just installed APC and armed your PHP with more deadly bullet. Later, we will discuss about how to use APC to build our sizzling upload progress bar.
Prior to writing the second part of this part, I would like to add some update so that this post will provide better info about the environment, system configuration and also troubleshooting.
1. Remark on APC installation on PHP 5.3.x
To the time this update was written, PHP 5.3.2 had been generally available. You may have been tempted to update from PHP 5.2 to PHP 5.3. Unfortunately, PHP 5.3 and PHP 5.2 were compiled with different Zend Engine version. This introduced backward-compatibility breaks for some extensions that are not packaged with the PHP bundle including APC. You may have encountered this error message when running command “phpinfo()”:
PHP Warning: PHP Startup: apc: Unable to initialize module
Module compiled with module API=20060613
PHP compiled with module API=20090626
These options need to match
The error message above shows that APC binary downloaded via yum was compiled with older API while your current PHP was compiled with a newer API targeted to PHP 5.3 and above.
If you’re running Fedora Linux (I tested on Fedora 12), there is some good news for you. The update repository has provided APC binary which is compatible with PHP 5.3.x. The package has also been renamed to php-pecl-apc. You can simply uninstall old php package and then reinstall the new one followed by APC installation via yum. This is achievable with simple commands on the terminal
root# yum remove php
root# yum install php
root# yum install php-pecl-apc
After invoking all the three commands, APC binary will have been installed and its configuration will have been stored as /etc/php.d/apc.ini (In case there is existing apc.ini, it will be installed as apc.ini.rpmnew)
2. APC module installation on Windows
It was mentioned that installation on Windows was easy. However, due to the nature of APC as an add-on, the DLL file is not shipped along with PHP bundle. I will elaborate a bit about how to get the DLL.
You can download the DLL from pecl4win.php.net or if the site is still under construction, you can navigate to Pierre Joye’s repository. Check if your PHP is thread-safe or non thread-safe and download the appropriate zip file based on your PHP installation type. Extract php_apc.dll from the zip file and put it into the PHP extension folder (usually ext folder in PHP installation directory). To enable the extension to support upload progress bar, add these lines into php.ini:
;Enable upload progress bar
;You can change the prefix and upload progress name below
Restart Apache and if you invoke phpini() function from command line or the browser, you will notice that the extension has been successfully installed.