Skip to content

File upload progress bars with PHP

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.

16 Responses

  1. very nice..

  2. Nico said

    very nice

  3. Marcus said

    the code presented is using the YUI framework not APC

  4. Marcus, the code is using YUI to create the progress bar and update it regularly. The server-side technology works with APC.

  5. You can install APC using PECL. Try to run PECL in your shell. If not working, install PECL through PEAR.

    You might need some libs -> look to APC pages on In comms. there are some useful hints.

  6. I’ve put together an improvement based on Rasmus Lerndorf’s example. This allows either single or multiple file uploads.

    Click here to view information and demos

    Also includes information on how to include other form fields along with the file upload.

  7. Dan said

    I can’t seem to get it to work with PHP 5.1, I get this error:

    php rfc1867 callback in Unknown on line 0

    my php.ini is updated according to the docs
    I added the apc.rfc1867 = 1

    Thus: Call to undefined function apc_fetch()

    - Did restart apache
    - Did compile and put the .so in the extension_dir
    - What am I missing?


  8. koushik said

    how to upload multiple files with this code?

  9. It’s best .but give me a full source code for education purpose.


  10. Prash said

    Nice articale, but how to see demo?

  11. Dipendra said

    dear All
    following code generate a fatal error on run time in php version 5.2.6
    please send me appropriate solution.

  12. The source code does not contain some of the javascript files where can i find them?

  13. fan said

    Here is a Tutorial on how APC works.

  14. Lionel said

    very nice

  15. Vinjith said

    Not Working on IE. is there any solution?

  16. Aρpreciate thiѕ post. Let me try it out.