PEAR Progress2 logo

HTML_Progress2 : The Definitive Guide

Server side

We will find at least two scripts: one, on final step, to manage temporary file upload

  1. // final destination of uploaded files
  2. $path = 'web_files/' ;
  3.  
  4. // basic PHP function file upload handler
  5. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  6.     if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
  7.         if (move_uploaded_file($_FILES['userfile']['tmp_name'], $path . $_FILES['userfile']['name'])) {
  8.             $html_output = 'Upload finished';
  9.             echo '<pre>'; var_dump($_FILES); echo '</pre>';
  10.         } else {
  11.             $html_output = 'Upload failed';
  12.         }
  13.     }
  14.     die($html_output);
  15. }

This chunk of code is found on examples/ajax/upload/apc5_upload1.php of the package distribution.

The second script will manage the polling loop iteration. Here we used an HTML_AJAX auto server. Example apc5_upload1.php used the basic HTML_Progress2_Upload::getStatus() method. This one return an array with four key-value pairs (percentage, labels, bytes_uploaded, bytes_total).

  1. <?php
  2. require_once 'HTML/AJAX/Server.php';
  3.  
  4. class AutoServer extends HTML_AJAX_Server
  5. {
  6.     // this flag must be set for your init methods to be used
  7.     var $initMethods = true;
  8.  
  9.     function initAPC5UploadStatus()
  10.     {
  11.         require_once 'HTML/Progress2/Upload.php';
  12.         $status = new HTML_Progress2_Upload();
  13.         $this->registerClass($status, 'APC5UploadStatus', array('getStatus'));
  14.     }
  15. }
  16.  
  17. $server = new AutoServer();
  18. $server->handleRequest();
  19. ?>

And you will ask: How to display other informations like percentage, filename, estimate time left and speed limit ?

You did it with progress bar labels and a format list. These formats are :

HTML_Progress2_Upload default behavior is to display an uplStatus label formatted as : "%C / %T (%P%)" on top left of progress bar.

[Note] Note

If you want to see this label information, don't forget to add it on HTML_Progress2 design.

  1. $pb = new HTML_Progress2();
  2. $pb->addLabel(HTML_PROGRESS2_LABEL_TEXT, 'uplStatus');
HTML_Progress2 : The Definitive Guide v 2.4.0 : April 20, 2007