// you’re reading...

dev

PHP Upload Progress with PHP 5.2 & APC

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.

Discussion

6 comments for “PHP Upload Progress with PHP 5.2 & APC”

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

    Posted by Koodough | November 2, 2007, 1:20 pm
  2. 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 ?

    Posted by Chad | December 18, 2007, 2:51 am
  3. 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.

    Posted by Elliot | December 18, 2007, 7:44 pm
  4. I can’t do it under PHP 5.2.5 for windows , when I upload file ,the Apache consume all the CPU resource ….

    Posted by Thaiki | April 28, 2008, 7:37 am
  5. 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.

    Posted by Jon | June 9, 2008, 4:59 pm
  6. I can think of an iframe that would update every n seconds.

    Posted by Fady Abdelhamid | August 18, 2008, 7:36 am

Post a comment