You would think that tracking file upload progress would be a given now, from your browser you can create websites, edit pictures and communicate with millions of people. Only a few sites will actually show you your upload progress and they are few and far between. If anything they will show you a nice animated gif that gives you the illusion your file is being transferred.
It is very possible to track upload progress but I have some bad news for some of you. If you are hosted on a shared server like Hostmonster, BlueHost, etc… then odds are you will not have the right privileges to install the required modules. For those of you that have access to your own server or the capability to get it installed then read on.
You can download the full source for this by clicking here. But I strongly recommend you read this!
Installing APC
First we need to install APC created by PECL. Unfortunately there is no longer support for this on windows based machines but you can still find the dll floating around the internet if you look hard enough. If you do get the dll and you want to use it with WAMP or any other type of local setup simply place it in your php folder with the rest of the dll’s under the “ext” folder. If you are installing remotely like on a server you can use the “wget” function to grab the files and the “gzip” command to extract. There are plenty of resources which can show you how to install, I will show you how to set it up and use it.
Windows Users: If the dll you find does not work try a different version, I had to try 3 different ones to get it to work on my local box.
Once you have the package or dll installed in your machine you will need to edit your php.ini file to configure some settings on apc. Search your php.ini file for “extensions” and you should be presented with a list of them, you need to add the following line:
extension=php_apc.dll
//CentOS or similar
extension = “apc.so”
This will activate the file in windows or on your server. Next you will need to instruct APC to monitor file uploads by adding the following line to the php.ini:
RFC 1867 is a document that describes how HTTP file uploads work for those of you that are curious. The final step in setting up APC is simply restarting your server.
Monitoring File Uploads With APC…The Code
So here is the thing, we actually need to use a combination of things to make this work correctly. First we need to set up our form and a unique key that can be used to track the file progress. We also need a few JavaScript / Ajax functions which will allow us to track the upload without refreshing the page. Finally we need a php function which will report the progress back to us so we can display it on the page. Below is an overview of how it will all work.
I will explain this step by step:

Step 1: We post our form with the file we are uploading, the data is collected by our JavaScript functions and we remain on the same page.
Step 2: The JavaScript will post all of the data to the php upload file and start our php progress indication.
Step 3: We use a JavaScript function to pull the upload progress from our php progress file, the progress is being tracked based on the file being uploaded in our php upload file.
Step 4: We update the files status in our progress area on the webpage.
Creating Our Form and a Key to Track Progress:
At the top of the page where our form resides we will need to generate a random string of characters which will be used by the APC function to track the files progress. We will need to include that value in our form, your form should look like the following:
<form name="upload" onsubmit="javascript:uploadFile();" action="upload.php" target="upload_target" method="post" id="ajax_upload" enctype="multipart/form-data">
<input type="file" name="myfile" id="myfile"/><br />
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $uniqueID; ?>" />
<input type="submit" value="Upload" />
</form>
<div id="progress"></div>
<iframe id="upload_target" name="upload_target" style="width:0;height:0;border:0px solid #fff;"></iframe>
There is our basic form, it is very important to leave the name of the apc input the way it is. Notice there is a DIV with the id of “progress”, this is the area we will display the upload progress. We are also including a hidden iframe so that the form will post there and not refresh the page. Also take note of the function call in the form tag, the onsubmit calls our JavaScript function to track the upload, we will write this later.
Now lets set up some of the JavaScript and Ajax. In order for us to tell if something is being posted back to us from any of our scripts we must listen for them. We have two different types of listeners here, one for IE and another for most other browsers.
Listener
if(window.XMLHttpRequest) {
HttpListener = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
HttpListener = new ActiveXObject("Microsoft.XMLHTTP");
}
We will use the above listener in our progress function.
Progress Function
if(HttpListener) {
HttpListener.open(‘GET’, ‘get_progress.php?uniqueID=’ + uniqueID, true);
HttpListener.onreadystatechange = function() {
if(HttpListener.readyState == 4 && HttpListener.status == 200) {
var response = HttpListener.responseText;
if (isNaN(response)){response=1; }
document.getElementById(‘progress’).innerHTML = Math.round(response) + "%";
if(response < 100) {
setTimeout(‘getProgress(‘+"’"+uniqueID+"’"+‘)’, 100);
}
else {
document.getElementById(‘progress’).innerHTML = "100% – Upload Complete!";
}
}
}
HttpListener.send(null);
}
}
What the above function does is send the uniqueID that we generated in the form to a file called get_progress.php(which we will write in a sec.) and listens for a response. When we receive a response from that file we update the results in our DIV progress back on our main page. You also see the “setTimeout” function, this calls the function over and over again which essentially posts to get_progress again and again thus updating our progress.
In order for this all to work we need one last piece of JavaScript, the actual function that sets the ball in motion:
var uniqueID = document.getElementById(‘progress_key’).value;
setTimeout(‘Progress("’ + uniqueID + ‘")’, 500);
}
This function grabs the uniqueID from the hidden input on the page and passes the value to our Progress function after half a second.
Progress:
The get_progress.php file mentioned above is very simple, here it is:
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header(‘Cache-Control: no-store, no-cache, must-revalidate’);
header(‘Cache-Control: post-check=0, pre-check=0′, FALSE);
header(‘Pragma: no-cache’);
if(isset($_GET[‘uniqueID’])){
$progress = apc_fetch(‘upload_’ . $_GET[‘uniqueID’]);
echo round($progress[‘current’]/$progress[‘total’]*100);
}
The get_progress.php file simply takes the uniqueID and calls the apc_fetch function which reports back an array of data about the file it is tracking. We are using “current” and “total” to come up with our percentage. The first four lines of this is to prevent some browsers from caching the original request, we force those to update each time this is called, otherwise the bar would appear frozen.
Last but not least, you need an upload.php script which will actually handle the file transfer, three lines and you are done:
$filepath = $filepath . basename( $_FILES[‘myfile’][‘name’]);
move_uploaded_file($_FILES[‘myfile’][‘tmp_name’], $filepath);
And that is it. When you break this code into sections and actually read how it all works, life becomes easy and this project becomes simple.
A note for those of you looking for a progress bar, the response sent from our get_progress.php is simply a number from 1 to 100. So you could take that number to create a very simple upload bar by creating a container DIV set to a width of 100px and place another container inside of that one set to an initial width of 0. You will need the internal div color to be set to whatever color you would like your bar and use JavaScript to update the inner div width based on the response.
For a progress bar on another site I used an animated gif as the background of my outer DIV and overlaid it with a transparent gif of the same dimensions for a really nice effect.
You can download the full source for this by clicking here. But I strongly recommend you read this entire article!
Good luck and have fun with this one.
![[del.icio.us]](http://scriptperfect.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://scriptperfect.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://scriptperfect.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://scriptperfect.com/wp-content/plugins/bookmarkify/facebook.png)
![[Furl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/furl.png)
![[Google]](http://scriptperfect.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://scriptperfect.com/wp-content/plugins/bookmarkify/linkedin.png)
![[MySpace]](http://scriptperfect.com/wp-content/plugins/bookmarkify/myspace.png)
![[Newsvine]](http://scriptperfect.com/wp-content/plugins/bookmarkify/newsvine.png)
![[Propeller]](http://scriptperfect.com/wp-content/plugins/bookmarkify/propeller.png)
![[Reddit]](http://scriptperfect.com/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://scriptperfect.com/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://scriptperfect.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://scriptperfect.com/wp-content/plugins/bookmarkify/technorati.png)
![[Twitter]](http://scriptperfect.com/wp-content/plugins/bookmarkify/twitter.png)
![[Email]](http://scriptperfect.com/wp-content/plugins/bookmarkify/email.png)
Sources…
[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……
You should check this out…
[...] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well [...]……
darmo seks…
Tutaj odnajdziesz stosunek seksualny bezplatnie….
Links…
[...]Sites of interest we have a link to[...]……
Read was interesting, stay in touch……
[...] listed below you’ll find the link to a few sites that we presume you really should have a look at [...]…
Its hard to find good help…
I am forever saying that its hard to procure good help, but here is…
mite…
[...]This problem does not need medications or doctor’s consultation. It can be cure d with home remedies[...]…
can be…
changes in different sectors which are depending on the variables. As a result of our existence have changed, the best way we spend our free time has…
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
Title…
This is my Excerpt…
Visitor recommendations…
[...]one of our visitors recently recommended the following website[...]……
Awesome website…
[...]the time to read or visit the content or sites we have linked to below the[...]……
the bike will…
is identical avenue for free on-line games. The designers of the games have thought of the changing instances and its needs…
Blogs ou should be reading…
[...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……
… [Trackback]…
Attractive component of content. I just stumbled upon your site and in accession capital to say that I get actually enjoyed account your blog posts. Anyway I’ll be subscribing in your feeds or even I achievement you get admission to persistently quick…
brown fields…
educative and but there are also websites that create a demon out of an angel. You need on-line games as a means of partaking the interests of your younger ones to be sure…
Great information…
This is often exceptional. Another looked at the about me subject matter when we are baffled. We are fascinated by this sort of offers. A particular appreciate your personal insert, and amount the effort inside this. Please keep cutting. They may be ma…
both the…
video games ever developed, you don’t have to carry any device with you moreover your pc, lap top,…
quality time…
that their stay online is safe. Moreover the security from viewing unhealthy sites, you also get to interact these people so much that they…
About thatarticle I read……
…check that out – we’re linking to……
and the…
In reality you might be most often if not on a regular basis provided with directions on methods to…
Sites You Should Check Out…
http://www.christchurchglass.getlisted.co.nz/double-glazing-retrofit...
Gems form the internet…
[...]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[...]……
It requires…
slightly resilience and a few effort to read the help info supplied for every game. Added to the truth that there isn’t any…
Wow!…
A very awesome post….
Title…
This is my Excerpt…
The other reason why moving house….
in Germany can be expensive is that getting free cardboard boxes is virtually impossible….
computer…
obtainable and that’s the reason as an adult it’s possible you’ll have to take some time to familiarize your self with what is accessible…
Great website…
[...]we like to honor many other internet sites on the web, even if they aren’t linked to us, by linking to them. Under are some webpages worth checking out[...]……
fix of estrogen replacement or other types…
of hrt, some of the best women’s clinics begin treatment by counseling women about diet, nutrition, lifestyle and dietary supplements. in many cases, making healthy changes in lifestyle and diet and adding nutritional supplements helps to increase a w…
Minibus sale…
[...]Sites of interest we’ve a link to[...]…
Cool sites…
[...]we came across a cool site that you might enjoy. Take a look if you want[...]……
Related……
[...]just beneath, are numerous totally not related sites to ours, however, they are surely worth going over[...]……
Gems form the internet…
[...]very few websites that happen to be detailed below, from our point of view are undoubtedly well worth checking out[...]……
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Very Good…
What a stuff of un-ambiguity and preserveness of valuable know-how on the topic of unexpected feelings….
Sites You Should Check Out…
http://www.drainsurgeons.getlisted.co.nz/auckland-drainlayer...
turning area…
smart telephone and even ipads.To be able to play free video games, you solely want internet entry and the talents or data to play….
Websites worth visiting…
[...]here are some links to sites that we link to because we think they are worth visiting[...]……
Sites You Should Check Out…
http://www.drainsurgeons.getlisted.co.nz/auckland-drainlayer...
Dota 2 beta…
I saw this really good post today….
Amazing blog…
I want to show you very interesting blog which I found on google….
Blogs ou should be reading…
[...]Here is a Great Blog You Might Find Interesting that we Encourage You[...]……
Great blog…
Today I found really great blog which has a lot of interesting posts to read…
Comment…
Your place is valueble for me. Thanks!……
Comment…
Your place is valueble for me. Thanks!……
Comment…
One of my favorite posts….
main path…
you purchase from the shops, you can’t be guaranteed you will get all you want everytime you want. But with free on-line games, you are…
………..
We suppose in the meantime i am going to be happy with bookmarking and also introducing your own Feed to our Search engines consideration. My partner and i anticipate fresh changes and definately will share this kind of website together with our Fb cla…