Elliot Haughin

I'm a freelance web developer.
I build clever, engaging, ass-kicking,
web applications. You want me?

email me, tweet me, skype me
Posted: Oct 23rd

After re-imaging my server which hosts FilePanda, I decided it would be a good idea to update PHP to allow me to use the new Alternative PHP Cache hook to get access to current upload file progress.

Sounds good right? = well, it’s much simpler than you would think.
First you’ve got to have PHP 5.2 installed on your system. For me this was easy:

yum update php

Then install apc for php:

yum install php-apc

Now it’s time to enable this hidden upload progress gem….
edit your /etc/php.ini file and add this line to the bottom:

apc.rfc1867 = on

All done? – Great… now we can start playing with the code.

Restart Apache, and get your editor out.

Our upload form will need to add a custom ‘ID’ to the form, which allows APC to pick out that individual file and send back the progress.

We need to add this hidden input to our form:

<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $unique_id?>"/>

With that added… we need to add a script which will get the current status of an individual upload:

<?php
if(isset($_GET['progress_key'])) {
 
  $status = apc_fetch('upload_'.$_GET['progress_key']);
  echo $status['current']/$status['total']*100;
 
}
?>

Now we hook them both up together… simply set the upload button to trigger a javascript like this:

<?php
   $unique_id = uniqid("");
?>
function getProgress(){
  GDownloadUrl("getprogress.php?progress_key=<?php echo($unique_id)?>", 
               function(percent, responseCode) {
                   document.getElementById("progressinner").style.width = percent+"%";
                   if (percent < 100){
                        setTimeout("getProgress()", 100);
                   }
               });
 
}
 
function startProgress(){
    document.getElementById("progressouter").style.display="block";
    setTimeout("getProgress()", 1000);
}

So, there’s the basics of how to get it going… it’s pretty simple really. For a full, and very good guide, see IBM’s guide.

Take a look at FilePanda’s upload progress bar for an example.

Related Posts

  1. Kick-Start Your Web Application With These Tools
Sponsored Links

Comments

  1. Koodough says:

    Could you give an example of a bare-bone php upload progress bar?

  2. Chad says:

    I would like to install this, but I don’t php-apc will work with ioncube correct ?

    I have to have ioncube is there a work around ?

  3. Elliot says:

    I’d say give it a go, if it doesn’t work with ioncube then I guess that’s one sacrifice that you just might have to accept to have encoded php code.

  4. Thaiki says:

    I can’t do it under PHP 5.2.5 for windows , when I upload file ,the Apache consume all the CPU resource ….

  5. Jon says:

    Very useful. I have updated my server, tried the code from IBM and it works, but want to do it without the Google dependency. Another variant I have found has Yahoo dependencies but don’t really want any dependencies. Any ideas on this would be welcome.

  6. Fady Abdelhamid says:

    I can think of an iframe that would update every n seconds.

  7. APC rfc 1867 suport only one file tracking you are so lam man

    Note that the file upload tracking is not threadsafe at this point, so new uploads that happen while a previous one is still going will disable the tracking for the previous.

    Only one file tracking in thredsafe. Not thredsafe PHP && APCHE you must by jokeing

    When using FastCGI and multiple PHP processes (recommended for performance reasons), you cannot use APC upload tracking.

    LAMER !!!

  8. Dave says:

    I’m using a windows 2003 server with apc installed and verified with phpinfo. In using the instructions found above along with the article published by IBM I have successfully got the upload script to work, however, when the progress bar appears it just sits there empty until the file has completed uploading and then the page simply states File Uploaded with no progression of the bar whatsoever. I’m getting no errors in my logs and as I have already stated the upload works fine, it’s just that I get zero progress. Anyone have any ideas?

  9. ert2 says:

    Big file == server script time out == no error

  10. Dave says:

    The file uploads fine, no time outs, no errors.

    It’s just that the progress bar does nothing!

  11. Ignacio says:

    Can you post the entire APC config for big files? And PHP config? Thanks.

  12. Adam says:

    Hmmm, yess.. another great blog entry where it says you CAN do something.. only.. I don’t know how so I’m gonna just tell you bits I read from somewhere else so it looks like I know but actually.. I don’t and secondly.. I can’t be arsed to actually explain….

    Great blog entry.. go go go..

  13. Shawn says:

    This example works perfectly thanks. Some things to note: Don’t listen to the idiot “PHPMasterBlaster” above… multiple progress bars work just fine. Threading has nothing to do with how this works. “Thread Safe” is a completely separate thing… do a search and find out for yourself. Secondly, in order to use APC for file upload prgress… you have to disable mod_security. There is a very simple reason for this. Mod Security is providing security by buffering requests and making sure they do not reach the application unless they have been checked and deemed safe.
    The apc upload progress script, on the other hand, relies on getting data in real-time. This are two contradicting requests.

  14. [...] I used APC and PHP5.2. If you are interested in the explanation of the process of APC and PHP5.2, Ellion Haughin’s blog entry might be a good starting point for [...]

  15. masikwha says:

    Shawn, lol, what works? Obviously this blog is copied text from IBM website. Matter of fact you can’t even download php_apc.dll(for windows), pecl says they are rebuilding the new version. How it worked for you and other is unknown. Necessary code is available in many sites, but what is not clear is how to install the apc .

Leave a Comment

© Copyright 2009 Elliot Haughin