File upload progress meter for PHP 4, at last!

Recommend this page to a friend!
  PHP Classes blog     File upload progress ...   Post a comment   See comments (35)   Trackbacks (5)  


Viewers: 517

Last month viewers: 6

Categories: PHP Tutorials, New site features

This post explains how to implement a upload progress meter for PHP sites, including those that still run PHP 4.

It also explains how to use a new plug-in of the Form Generation and Validation class to show an upload progress bar and live upload statistics using AJAX COMET requests.

The Forms Generation and Validation class is very important to the PHPClasses site since its beginning. This post tells about the story of the site and how this class motivated its creation.


- The story of the PHPClasses site and the forms class
- Form file upload progress meter
- File upload progress meter for PHP 4
- Upload progress meter plug-in
- Merry X-mas and Happy New Year

This year has been very intense in terms of PHP developments, at least for me. I could talk about the the upcoming premium services, that are almost ready for launch. I could also talk about several PHP conferences that I have attended as a speaker. But no, I have already talked a lot about those subjects in previous posts.

In this end of year post, I will be talking about a single PHP class. Not just any class, but the class that made me create the PHPClasses site: Forms Generation and Validation.

But let me tell you why this class is so important to PHPClasses site and also tens of thousands of PHP developers that use it.

- The story of the PHPClasses site and the forms class

I have started working with PHP around 1997, still in the PHP 2 beta days. I used to participate in PHP mailing lists a lot, like for instance in the php-general list. I still do, but not as frequently as then. I was learning about PHP and exchanging experiences with other PHP users.

When PHP 3 was launched in 1998 in opened up a new world of possibilities with the introduction of basic Object Oriented Programming (OOP) support. OOP classes provide a much better way to organize functions and variables associated to objects. It allows developers to encapsulate functionality that facilitates reuse and sharing code between projects, and also sharing with other people.

In January 1999 I started working on a class that would solve a recurring need of Web applications: generating and validating Web forms. The main goal was to generate HTML with embedded Javascript that would allow me to quickly deploy Web based forms.

The class should be able to validate forms on the server side, as well generate all the Javascript code to perform the same validations on the browser side, without requiring me to write Javascript by hand. This was one of the most important productivity factors that motivated the development of this class.

Once in while someone popped in the php-general mailing list asking about solutions to generate and validate forms. Then I talked about my class and suggested to contact me off the list, so I could share the class code. When the users contacted me, I could keep track of their e-mail addresses, so I could notify them later when a new version was available.

One day of June 1999, one of the early class users from Australia, named Robert Chalmers, after receiving the class from me by e-mail, was so happy about it that he posted back to the list praising the class. ...

When I saw that message I was stunned and blushed. I was not exactly looking for public recognition, although it was very nice of Robert. I was mainly interested to get as many users as possible to test the class, and hopefully report bugs and suggest new features. ...

Anyway, what followed was even more overwhelming. All of the sudden, I started receiving tens of requests from other users that followed Robert's advice and contacted me to get a copy of the class.

It was nice but it quickly became inviable to distribute the class by e-mail to anyone that wanted it. That made me look for a good online repository where I could distribute the class and keep the users in touch to be notified about new versions.

I looked at PX - PHP Code Exchange, written by David Sklar, which by the way it is still running until today.

PX was almost perfect. The only problem is that it had no way to keep track of the users that downloaded the scripts, so they could be notified when a new version was released.

I contacted David and proposed to implement that feature. He was not interested, despite I offered myself to develop the necessary code. That is ok. After all it is his site and he is the one to decide what is or is not interesting. So, I respected his decision.

Then I decided to implement my own repository. In less than a week after I started, the PHP Classes Repository site was finally launched. ...

In the beginning it was meant only to distribute my own classes, but some time later I opened it to accept contributions by others. The rest is history.

- Form file upload progress meter

The forms class has been greatly enhanced over the years. In 2004 I added the ability to implement new types of inputs using plug-in classes. More recently I have developed the ability to submit forms using AJAX requests, which allowed to implement sophisticated controls like linked select inputs and auto-complete text inputs.

Last November PHP 5.2 was released. It finally integrated a patch originally developed by Doru Petrescu. It was submitted to the PHP core developers in 2003. ...

I have no idea why it took more than 3 years to accept and adapt the patch, as being able to monitor file upload progress was one of the most wished features by many PHP users.

Anyway, PHP 5.2 alone only provides the hooks to monitor file upload progress. To actually monitor the progress, it is necessary to use an extension that retrieves upload progress statistics and makes it available to another PHP script running in parallel.

Meanwhile Christian Stocker has submitted upload progress meter extension also based on the original work contributed by Doru. ...

The actual extension is currently available from the PECL repository:

Christian also announced the Windows version of this extension as a pre-compiled DLL: ...

- File upload progress meter for PHP 4

The upload progress meter feature is great, especially for sites that take large file uploads. The problem is that almost 90% of the PHP sites still use PHP 4 and have no plans to upgrade the PHP 5.2 any time soon.

As a side comment, I would like to mention that I am intrigued of why certain PHP core developers still wonder why PHP 5 adoption does not pick up. ...

The way I see it, the reason to not move to PHP 5 is simple: cost. It costs a lot of time and money to audit and change the code of existing sites to make them work with PHP 5 changes.

I am not only talking about documented changes. I am also talking about a lot of undocumented backwards incompatible changes that make an hell to everybody that needs to migrate reasonably sized sites.

Since a lot of developers and site owners do not want to migrate to PHP 5, hosting companies also do not migrate their servers currently running PHP 4, or else it will cost them a lot with the eventual loss of customers annoyed with their sites broken with PHP 5.

I am afraid that the whole PHP 5 version series is doomed to never pickup. I suspect that the actual growth of PHP 5 is less than the growth of the overall PHP adoption. Therefore, my advice to PHP core developers is to focus on PHP 6, and this time give up any ideas of making backwards incompatible changes.

The PHPClasses site is one of those sites that uses PHP 4 and is not expected to migrate to a newer version soon, especially because it is over 100,000 lines of code. Large code base sites not migrating to PHP 5 is normal. Even Zend has sites running on PHP 4 that they do not intend to migrate to PHP 5. ...

Since I wanted to use this upload progress meter extension under PHP 4, I had to find an alternative solution. I decided to port the patch that adds hooks to monitor upload progress to make it run under PHP 4.

If you are interested, you may find the patch to make it work under PHP 4.3.11: ...

and also under PHP 4.4.4: ...

Just let me know if you would like to have a patch that works with other PHP versions, or if you have difficulties to install these patches.

- Upload progress meter plug-in

Since you file upload uses forms, it is only natural for me to develop a plug-in for the forms class to provide any easy to deploy upload progress meter solution. I have been developing that plug-in recently and I have just released it as part of the forms class distribution.

The upload progress meter already shows in the package file upload pages of the PHPClasses site. If you want to see it in action, you may try it here in this forms examples page: ...

Here you can view the example code to see how easy it is, all without writing a single line of Javascript: ...

The plug-in uses COMET based AJAX solution to provide fast progress bar update. COMET is a much better solution for this situation than using XMLHttpRequest based AJAX.

COMET requests only establish one HTTP connection. There is no need to keep polling the server to get updates, like when you use XMLHttpRequest. With COMET, the upload monitor HTTP request is kept running until the upload finishes. This way the progress bar and statistics updates are sent very quickly to the browser.

As I also recommended in a past post that if you use mod_gzip, you may need to turn off compression for the pages that serve the upload progress requests. Otherwise, mod_gzip buffers the whole COMET response and no progress bar updates are seen. ...

- Merry X-mas and Happy New Year

Well, it is almost X-mas and it is time to rest for while, for me and to everybody that has been working hard to make PHP the best solution to solve your Web problems.

I will try to respond in the next days any questions that you may have about these subjects, but if I do not speak to you sooner, have a Merry X-mas and Happy New Year 2007 .

Manuel Lemos

You need to be a registered user or login to post a comment

1,561,245 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



6. Path for php 4.4.6? - Dennis Daudelin (2008-06-29 01:17)
different version of php... - 1 reply
Read the whole comment and replies

4. POST sent by Browser - Santosh Nagarajan (2008-03-20 05:23)
Help required on getting text data from a browser... - 13 replies
Read the whole comment and replies

5. PHP File Upload Progress for php 4.4.2 - vinu mathews (2008-01-15 07:54)
Which all files need to be patched... - 1 reply
Read the whole comment and replies

3. php 4.4.2 patch - Roberto de la Torre (2007-08-02 17:07)
Patch needed... - 0 replies
Read the whole comment and replies

1. Patch for 4.4.3 - Hammad Tariq (2007-07-16 23:50)
I need a patch for 4.4.3... - 13 replies
Read the whole comment and replies

2. HOw do you use this patch? - Douglas Brown (2007-04-24 16:56)
Not sure where to go from here?... - 1 reply
Read the whole comment and replies


5. How to implement a file upload progress meter with PHP (2010-07-13 06:38)
With many web applications it is a common requirement that users are allowed to upload files. If the files are large however, this will take some time, and it's best to be able to make the user aware of the file progress...

4. Connaître la progression d un chargement (upload) de fichier en PHP (2009-01-18 16:29)
Lorsque l’on conçoit un formulaire permettant l’upload de fichier, il est souvent souhaité de pouvoir indiquer à l’utilisateur la progression du chargement que ce soit sous forme graphique ou texte...

3. File upload progress bar odprta koda / open source» Blog Archive » File upload progress bar (2007-01-25 00:03)
Here’s another AJAX based application that can produce a usefull output with uploaded bytes informations...

2. File upload progress meter for PHP 4, at last! (2006-12-21 07:29)
With the release of PHP 5.2 and a new feature, the file upload hooks useful for making an upload progress meter, PHP 4 users have been feeling a little left out...

1. PHP file form meter (2006-12-21 04:33)
Sådan en class der kan vise hvor langt man er med at uploade en fil. Ligesom i windows og sådan.

  PHP Classes blog     File upload progress ...   Post a comment   See comments (35)   Trackbacks (5)  

For more information send a message to info at phpclasses dot org.