Creating an Upload Progress Bar in PHP with APC
Introduction
In this tutorial I will explain how to create a progress bar for PHP file uploads. There is no method build into PHP for returning the status of an upload in progress, so this requires a module called the Alternative PHP Cache (APC). This allows you to store data across sessions and includes built-in functionality for storing/returning file upload stats.
This tutorial assumes you have some basic understanding of XHTML, PHP, and JavaScript. If you don’t, it may be hard to understand.
Installing APC is beyond the scope of this tutorial, so visit the APC Installation page for help or talk to your host. To find out if APC is installed already, create a PHP file on your site containing “<?php phpinfo(); ?>“, visit it in your browser, and look for a section titled ‘APC.’ If it is present, then it is installed.
Similar Posts:
- Saving Time With jQuery
- Toggle Display of Page Elements with JavaScript
- Reloading Images Using JavaScript
« Previous Next »
Tags: AJAX, file uploads, JavaScript, PHP, progress bar

February 9th, 2009 at 1:27 pm
Hello,
this the best tutorial about APC upload I have ever seen. Unfortunately, it doesn’t work to me. When I upload file, apc almost immediately return {finish:true;}, therefore file isn’t uploaded yet. I have read about this problem (bug) something, but it is explained very well nowhere. Do you have survey in which cases (server configuration) it happens?
Thank you a lot.
tomasr
February 11th, 2009 at 12:51 pm
Make sure you have this in your PHP configuration to enable upload tracking:
apc.rfc1867 = OnFebruary 13th, 2009 at 5:33 am
Hello,
this upload bar is excellently working in FF, but I have noticed, that in IE (both 6 and7) it doesn’t work. It just returns error, that object was expected @ line XY (I think it is line with , but it is in IE, so who knows…)
Do you know what I (if it is me) am doing wrong?
Jen
February 13th, 2009 at 11:08 am
Actually this was an error on my part. When including the script on the page, the type should be text/javascript not application/javascript. I’ve corrected the tutorial to reflect that.
Sorry for the confusion
February 21st, 2009 at 1:13 pm
Yes I’m sure that I’ve apc.rfc1867 = On in php.ini on localhost and production server as well.
I don’t know why it doesn’t work on localhost. Only one reason, why it doesn’t work on production servver is that php is there compiled like fastcgi and I’ve ever read, that in this case apc doesn’t work.
Any idea?
February 23rd, 2009 at 9:29 pm
I can’t find any information on any configuration that would cause that. It’s probably something in your script.
February 26th, 2009 at 8:00 am
http://www.ibm.com/developerworks/opensource/library/os-php-v525/index.html
IBM recommends “Using a text editor, open /php/php.ini and add the line apc.rfc1867 = on (it doesn’t matter where). If you’re trying to test locally and plan to upload large files so you can actually see progress, you’ll also want to add the following directives: apc.max_file_size = 200M, upload_max_filesize = 200M, and post_max_size = 200M. Don’t do this on a live production server, though, or you’re likely to use up bandwidth and disk space allotments, not to mention slowing everyone else down to a crawl.” <- WAMP
for localhost tests. I dont know if this will help or not.
March 7th, 2009 at 1:37 am
I am completely lsot this about about the most amazing tutorial I have ever seen yet for some reason i cannot get it working.
I want to use this script for uploading videos to a website i am building but cant get a local demo running for the life of me!!!!
Any Ideas????
http://fadetoclear.com/uploader/upload_form.php
March 9th, 2009 at 2:55 am
hi!
it works with multiple uploads?
i would like to upload 3 files at time.
March 9th, 2009 at 9:55 am
Yes, it should work fine with multiple files. Just add more files fields to the form and name them
file[]so PHP will treat them as an array. There is an example showing how to handle multiple files at: http://www.php.net/manual/function.move-uploaded-file.php#function.move-uploaded-file.examplesMarch 18th, 2009 at 7:32 am
Thanks for a very useful tutorial. I truly appreciate it.
In case this will help others in the future. I had to change
from $_FILES['userfile']['name'] to $_FILES['file']['name']
and from $_FILES['userfile']['tmp_name'] to $_FILES['file']['tmp_name']
in order for the code to work.
March 18th, 2009 at 11:06 am
Good catch! I corrected the tutorial. Copy/paste error…
June 29th, 2009 at 2:15 pm
Thanks for the tutorial. Works excellent!!!!
How do i make the progress bar uploading to be done in a separate window? and when i try uploading files of larger size i get a division by zero warning and the progress bar stops working. Could you please help me out with these two issues.