Script Perfect

         Random snips of code and bugs

PHP File Upload Progress (APC Fetch PECL)

Posted by Tim On September - 4 - 2009

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:

//WINDOWS USERS:
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:

apc.rfc1867 = On

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:

<?php $uniqueID = md5(uniqid(rand())); ?>
<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

var HttpListener = false;
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

function Progress(uniqueID) {
   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:

function uploadFile(){
        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(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’);
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 = "uploads/";
$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.

4,328 Responses to “PHP File Upload Progress (APC Fetch PECL)”

  1. Hi there may perhaps I reference some with the insight the following in this blog if I reference you with a link back again for your web page?

  2. NIKE OUTLET says:

    I wants to thank you to the endeavors you’ve produced in publishing this content. I am trusting the same best perform from you inside the long term as well. In fact your fanciful writing abilities has inspired me to start my very own blog now.

  3. What a wonderful resource!…

  4. NIKE OUTLET says:

    I have been to your site half a dozen times now, and this time I am adding it to my bookmarks.

  5. **YOUTUBE VIDEO REVIEWS ON THE HOTTEST ELECTRONICS OUT**…

    GET THE EXCLUSIVE REVIEW’S ON THE HOTTEST ELECTRONICS OUT IN THE MARKET RIGHT NOW!…

  6. This can be a very good website write-up and I defer to you what you could have mentioned right here. I have previously subscribed in your RSS feed in Firefox and are going to be your regular reader. Thanks for the time in writing the article….

  7. Great web site, thanks a great deal with the awesome posts!…

  8. cheap uggs says:

    Great article. Thank you to tell us more useful information. I am looking forward to reading more of your articles in the future.

  9. Your point is valueble for me. Thanks!…

  10. What a excellent resource!…

  11. Wonderful weblog, many thanks a good deal for the amazing posts!…

  12. Many thanks to the person who made this post, this was very informative for me. Please continue this awesome work. Sincerely…

    The article is very well!

  13. I’ve been looking everywhere for this! Thank goodness I found it on Bing.Thx

  14. ED Hardy says:

    What a fun pattern! It’s great to hear from you and see what you’ve sent up to. All of the projects look great! You make it so simple to this.Thanks!

  15. With great pleasure I read your blog. The information is very interesting. My gratitude to you.

  16. Gucci Outlet says:

    As usual this was a thoughtful post these days. You make me desire to retain coming back again and forwarding it my followers…….

  17. duvetica says:

    What an interesting and info packed site. Thanks for this, I really appreciate what you have done here. Keep it up, and I will be back for more. Bill Kilner…
    duvetica
    duvetica jackets
    duvetica thia
    duvetica vest

  18. I would like to say “wow” what a inspiring post. This is really great. Keep doing what you’re doing!!…

    Intriguing post. I have been searching for some good resources for solar panels and discovered your blog. Planning to bookmark this one!…

  19. Some time before, I needed to buy a building for my firm but I did not earn enough cash and could not purchase anything. Thank goodness my colleague suggested to try to get the credit loans from banks. Hence, I did that and was happy with my college loan.

  20. Wholesale Yankee Candles…

    [...]just below, are some totally unrelated sites to ours, however, they are definitely worth checking out[...]…

  21. Online Movie Downloads…

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  22. Download Full Movies…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  23. Employment JObs…

    [...]here are some links to sites that we link to because we think they are worth visiting[...]…

  24. [Trackback from relevant blog]…

    …Saw this recently so I thought I’d share this post……

  25. A.J. Burnett says:

    Chandler homes for sale…

    [...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…

  26. Potenzmittel…

    [...]Potenzmittel online kaufen. Viagra, Cialis, Levitra rezeptfrei bestellen[...]…

  27. Scrapebox List…

    [...]Auto Approve List[...]…

  28. Steve’s Blog…

    [...]just under, are a few completely unrelated sites to ours, however, they are most definitely worth going over[...]…

  29. Tax Liens says:

    Help With IRS Wage Garnishments…

    below are a few links to webpages which we link to mainly because we think they are worthwhile visiting…

  30. Google News…

    [...] on this page there’s the hyperlinks to a new internet sites that take into account you may want to come to [...] …

  31. New Indie Films Online Says It’s An Incredible Posting Well Done…

    Terrific news; thought we would include a few not related posts, but still worth checking them out… fyi have you hear regarding Middle East now has much more complications too !…

  32. four says:

    Baby Bedding…

    [...]here are some links to sites that we link to because we think they are worth visiting[...]…

  33. Live Blogs…

    [...] followed below there can be the website link towards a world-wide-web sites i visualise be certain to use [...] …

  34. workout routines to build muscle…

    [...]just below, are some totally unrelated sites to ours, however, they are definitely worth checking out[...]…

  35. Barnevelder Chicken…

    [...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…

  36. Pepper Mints says:

    Cheers, enjoyed the read…

    [...]I link out rarely, but this was praiseworthy. Bravo, hope we can work together in the future[...]…

  37. Travel Offers…

    [...]The subsequent websites are numerous websites which attracted our management, thus remember to have a look at all of them[...]…

  38. Find LED Bulbs by Color…

    [...]the time to study or go to the content material or sites now we have connected to below the[...] …

  39. Customer Service Software…

    [...]here are some links to sites that we link to because we think they are worth visiting[...]…

  40. …Notification of usage……

    [...This trackback notifies you of the usage of...]…

  41. Buy Electric Guitar Packages…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  42. [...]below you’ll find the link to some sites that we think you should visit[...]…

    [...]here are some links to sites that we link to because we think they are worth visiting[...]…

  43. Background Check…

    [...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…

  44. First student loan…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  45. Flax Seeds Benefit…

    [...] the time to read or visit the content or sites we have linked to below the. Posted in [...]…

Leave a Reply

Spam protection by WP Captcha-Free

About Me

I am an independent web developer and webmaster of many sites. The main goal of Script Perfect is to provide answers to some of the hard to find questions when it comes to website design and coding.

Twitter