Creating an Upload Progress Bar in PHP with APC

  1. Introduction
  2. Create the Form
  3. The PHP Scripts
  4. The JavaScript (AJAX)
  5. The Finished Product
  6. View All

Introduction

In this tutorial I will explain how to create a progress bar for PHP file uploads. There is no method build into PHP for returning the status of an upload in progress, so this requires a module called the Alternative PHP Cache (APC). This allows you to store data across sessions and includes built-in functionality for storing/returning file upload stats.

This tutorial assumes you have some basic understanding of XHTML, PHP, and JavaScript. If you don’t, it may be hard to understand.

Installing APC is beyond the scope of this tutorial, so visit the APC Installation page for help or talk to your host. To find out if APC is installed already, create a PHP file on your site containing “<?php phpinfo(); ?>“, visit it in your browser, and look for a section titled ‘APC.’ If it is present, then it is installed.

Similar Posts:

 
  • Share/Save/Bookmark

« Previous  Next »

Pages: 1 2 3 4 5

Tags: , , , ,

13 Responses to “Creating an Upload Progress Bar in PHP with APC”

  1. tomasr Says:

    Hello,
    this the best tutorial about APC upload I have ever seen. Unfortunately, it doesn’t work to me. When I upload file, apc almost immediately return {finish:true;}, therefore file isn’t uploaded yet. I have read about this problem (bug) something, but it is explained very well nowhere. Do you have survey in which cases (server configuration) it happens?
    Thank you a lot.

    tomasr

  2. UltraMega Says:

    Make sure you have this in your PHP configuration to enable upload tracking:
    apc.rfc1867 = On

  3. jen.novotny2 Says:

    Hello,
    this upload bar is excellently working in FF, but I have noticed, that in IE (both 6 and7) it doesn’t work. It just returns error, that object was expected @ line XY (I think it is line with , but it is in IE, so who knows…)
    Do you know what I (if it is me) am doing wrong?

    Jen

  4. UltraMega Says:

    Actually this was an error on my part. When including the script on the page, the type should be text/javascript not application/javascript. I’ve corrected the tutorial to reflect that.

    Sorry for the confusion

  5. tomasr Says:

    Yes I’m sure that I’ve apc.rfc1867 = On in php.ini on localhost and production server as well.
    I don’t know why it doesn’t work on localhost. Only one reason, why it doesn’t work on production servver is that php is there compiled like fastcgi and I’ve ever read, that in this case apc doesn’t work.

    Any idea?

  6. UltraMega Says:

    I can’t find any information on any configuration that would cause that. It’s probably something in your script.

  7. renner Says:

    http://www.ibm.com/developerworks/opensource/library/os-php-v525/index.html

    IBM recommends “Using a text editor, open /php/php.ini and add the line apc.rfc1867 = on (it doesn’t matter where). If you’re trying to test locally and plan to upload large files so you can actually see progress, you’ll also want to add the following directives: apc.max_file_size = 200M, upload_max_filesize = 200M, and post_max_size = 200M. Don’t do this on a live production server, though, or you’re likely to use up bandwidth and disk space allotments, not to mention slowing everyone else down to a crawl.” <- WAMP

    for localhost tests. I dont know if this will help or not.

  8. fade2clear Says:

    I am completely lsot this about about the most amazing tutorial I have ever seen yet for some reason i cannot get it working.

    I want to use this script for uploading videos to a website i am building but cant get a local demo running for the life of me!!!!

    Any Ideas????

    http://fadetoclear.com/uploader/upload_form.php

  9. fre2004 Says:

    hi!
    it works with multiple uploads?
    i would like to upload 3 files at time.

  10. UltraMega Says:

    Yes, it should work fine with multiple files. Just add more files fields to the form and name them file[] so PHP will treat them as an array. There is an example showing how to handle multiple files at: http://www.php.net/manual/function.move-uploaded-file.php#function.move-uploaded-file.examples

  11. smithf1000 Says:

    Thanks for a very useful tutorial. I truly appreciate it.

    In case this will help others in the future. I had to change
    from $_FILES['userfile']['name'] to $_FILES['file']['name']
    and from $_FILES['userfile']['tmp_name'] to $_FILES['file']['tmp_name']
    in order for the code to work.

  12. UltraMega Says:

    Good catch! I corrected the tutorial. Copy/paste error…

  13. Santhosh Kumar Says:

    Thanks for the tutorial. Works excellent!!!!

    How do i make the progress bar uploading to be done in a separate window? and when i try uploading files of larger size i get a division by zero warning and the progress bar stops working. Could you please help me out with these two issues.

Leave a Reply

You must be logged in to post a comment.