Professional Freelance Web Developer
CodeIgniter Activist, Caffeine Junkie

Elliot Haughin

PHP Upload Progress with PHP 5.2 & APC

23rd October 2007

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
  2. Building UTF8 Compatible CodeIgniter Applications

  • Hey,

    This is an very good post to understand the new concept of PHP 5.2.
    I was also in search of this type of file upload prgressbar.
    Thanks again for the valuable information.
  • I know a way to do this without installing anything, just plain PHP and a DB table to store update information, nothing more, I have a demo here

    http://burning-candle.com/projects/chris/monito...

    (Yes, there are bugs, but they are bugs in the user interface, not bugs in the idea, the idea is rock solid)
  • Glad the rest of the world finally found this nifty little hook! For anyone having issues uploading large files and getting a time-out here are a few php.ini settings that you should make sure you have setup. (Note php has a hard limit max upload file-size of 2gb in most php builds).

    [php.ini]
    ;APC
    apc.rfc1867 = on
    apc.rfc1867_freq = 1K
    apc.gc_ttl = 259200
    apc.max_file_size = 2000M

    ;File Upload
    file_uploads = On
    upload_max_filesize = 2000M
    post_max_size = 2000M

    [end php.ini]

    Note: (If you are using Suhosin then add the following var)

    ;Shoshin APCFix
    suhosin.post.max_value_length = 2000M

    Hope this helps someone. By the way great post!
  • titan
    Me gusta mucho pero seria bueno que pusiera un ejemplo para descargar.. =)
  • Alex
    To install APC for PHP 5.2.10 for example, follow these steps:
    - go to http://downloads.php.net/pierre/ and download the appropriate package (e.g. php_apc-3.0.19-5.2-Win32-VC6-x86.zip for PHP 5.2 thread safe);
    - extract php_apc.dll to c:\php\ext (or whatever extensions folder you have set);
    - open php.ini and add this line:
    extension=php_apc.dll, then save the file;
    - restart Apache;
    - check if APC was enabled - use phpinfo() for that (you should see new APC section somewhere in the displayed output) - or check the error.log of Apache to make sure the extension was successfully loaded (if not loaded, an error message will be shown there);

    That's all folks (it worked for my PHP 5.2.10 Win32 instance).
    Good luck!
  • masikwha
    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 .
  • 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.
  • Adam
    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..
  • Can you post the entire APC config for big files? And PHP config? Thanks.
  • The file uploads fine, no time outs, no errors.

    It's just that the progress bar does nothing!
  • ert2
    Big file == server script time out == no error
  • 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?
  • 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 !!!
  • Fady Abdelhamid
    I can think of an iframe that would update every n seconds.
  • Jon
    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.
  • Thaiki
    I can't do it under PHP 5.2.5 for windows , when I upload file ,the Apache consume all the CPU resource ....
  • Elliot
    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.
  • Chad
    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 ?
  • Could you give an example of a bare-bone php upload progress bar?
blog comments powered by Disqus

Boring Stuff

Design © copyright Elliot Haughin 2009

Content published here are copyright their respective owners.

You cannot copy content from this site, either in English or translated to another language.

Keep Subscribed

Theres lots of ways for you to keep up with me on the web.

Please Note

Information given out on this blog should only be used as a guideline. I hold no liability for any code I write.

Always consult a professional before acting on this guidance.