There is a nifty example by Rasmus Lerdorf about using file upload hooks in PHP 5.2.x to generate a progress bar while uploading a file through a HTML form. The source code is pretty self-explaining regarding all the things that happen on the client side, but unfortunately Rasmus does not mention what is required on the server side to make the magic work.

In order to make it possible for clients to regularly poll a server for the progress of an upload, one needs a bit of middleware that hooks into the right places inside PHP and exports the progress information to “userland” PHP code. In Rasmus’ case, he is using the APC opcode cache to do this. Recent versions of it come with a (yet undocumented) feature which, once enabled, listens for incoming file upload requests that contain a special field called APC_UPLOAD_PROGRESS. If this field is given, APC creates a cache entry named after the value of the field and regularly populates the file upload progress to this entry.

If you look closely at Rasmus’ example, you’ll notice that not only the script uses apc_fetch() in order to read information from a shared memory segment, but also that it includes the magic APC_UPLOAD_PROGRESS field. The value of the field is generated using the uniqid() function so that multiple file uploads happening at the same don’t use the same cache entry.

So how does one trick APC into working that way? The documentation does not say a word about it at the time of writing this, but the installation instructions in the CVS repository tell us that one needs to add apc.rfc1867 = 1 to php.ini in order to enable the hooking. Once you know that, everything works just magically.

Hope this helps.

A Martin Jansen production. ⋅ © 2006-2008 ⋅ Licensed under a Creative Commons Attribution-ShareAlike 2.5 license unless otherwise noted.