Examples TOCexamples

Web-HTTP ProgressBar uploader

$Date: 2005/07/25 11:43:56 $

 Table of contents

Introduction

This example requires :


This example allows you to upload files from a web browser to your web server.

PEAR::HTML_QuickForm package is used to make and manage the form that will send file to your web server (right frame), while HTML_Progress is only used to display a vertical progress meter running in indeterminate mode (left frame).

Be aware that your web server could returns a timeout on long upload operation, by running the HTML_Progress script (See technical notes below).

[Top]

 Technical notes

The script time-out refers to the number of seconds a PHP page is given before the script is assumed to have failed and the page terminated. If you are uploading a large file the script that is receiving the transfer may time out before the file has been completely uploaded. To alter this setting you should insert the following code into your uploading page prior to creating any HTML_Progress and HTML_QuickForm objects.

1 <?php set_time_limit(300); // five minutes ?>

See also: set_time_limit manual.

[Top]

 Render options

increment = 5 (to make progress bar animation smoothest in indeterminate mode)
HTML_Progress::setIncrement()
speed = 100 (to make progress bar animation smoothest)
HTML_Progress::setAnimSpeed()
color = #000000
with  = 1px
HTML_Progress_UI::setBorderAttributes()
active-color   = #970038
inactive-color = #FFDDAA
width          = 20
height         = 20
HTML_Progress_UI::setCellAttributes()
count = 20
HTML_Progress_UI::setCellCount()

[Top]

 Output

Screenshot

[Top]

 PHP source syntax highlight

The form file selection:

  1. <?php
  2. /**
  3.  * Mixed Field Upload
  4.  * This example shows how to upload a form containing a mix of standard form
  5.  * and file input fields.
  6.  *
  7.  * @version    $Id: mixedupload.php,v 1.3 2005/07/25 11:43:56 farell Exp $
  8.  * @author     Laurent Laville <pear@laurent-laville.org>
  9.  * @package    HTML_Progress
  10.  * @subpackage Examples
  11.  */
  12.  
  13. require_once 'HTML/QuickForm.php';
  14.  
  15. function myProcess($values)
  16. {
  17.     global $form;
  18.     $destination = './uploads/';
  19.  
  20.     $file =& $form->getElement('tstUpload');
  21.     if ($file->isUploadedFile()) {
  22.         $ok = $file->moveUploadedFile($destination);
  23.  
  24.         if ($ok) {
  25.             // write the semaphore to tell progress meter to stop
  26.             // in script 'progressbar.php'
  27.  
  28.             $fp = fopen($destination . $_GET['ID'],'w',false);
  29.             fwrite($fp, 'done');
  30.             fclose($fp);
  31.         }
  32.     }
  33. }
  34. ?>
  35. <html>
  36. <head>
  37. <script language="javascript">
  38. <!--
  39. function DoUpload() {
  40.   theUniqueID = (new Date()).getTime() % 1000000000;
  41.   parent.meter.window.location = "vbar.php?ID=" + theUniqueID;
  42.   parent.files.mixed.action = "mixedupload.php?ID=" + theUniqueID;
  43.   parent.files.mixed.submit();
  44. }
  45. //-->
  46. </script>
  47. </head>
  48. <body>
  49. <?php
  50.  
  51. $form =& new HTML_QuickForm('mixed');
  52.  
  53. // We need an additional label below the element
  54. $renderer =& $form->defaultRenderer();
  55. $renderer->setElementTemplate(<<<EOT
  56. <tr>
  57.     <td align="right" valign="top" nowrap="nowrap"><!-- BEGIN required --><span style="color: #ff0000">*</span><!-- END required --><b>{label}</b></td>
  58.     <td valign="top" align="left">
  59.         <!-- BEGIN error --><span style="color: #ff0000">{error}</span><br /><!-- END error -->{element}
  60.         <!-- BEGIN label_2 --><br/><span style="font-size: 80%">{label_2}</span><!-- END label_2 -->
  61.     </td>
  62. </tr>
  63.  
  64. EOT
  65. );
  66.  
  67. $form->setDefaults(array(
  68.     'color'     => 'orange'
  69. ));
  70.  
  71. $form->addElement('header', null, 'Uploaded file rules');
  72. $form->addElement('file', 'tstUpload', array('What is your favorite picture ?', 'Rule types: \'uploadedfile\', \'maxfilesize\' with $format = 512000, <br />filename with $format = \'/\.(jpe?g|gif|png)$/\'<br />Validation for files is obviuosly <b>server-side only</b>'));
  73. $form->addRule('tstUpload', 'Upload is required', 'uploadedfile');
  74. $form->addRule('tstUpload', 'File size should be less than 500kb', 'maxfilesize', 512000);
  75. $form->addRule('tstUpload', 'File name should be *.jpg, *.gif or *.png', 'filename', '/\.(jpe?g|gif|png)$/i');
  76.  
  77. $form->addElement('header', null, 'Assortment of other fields');
  78. $form->addElement('text', 'color', 'What is your favorite color ?');
  79. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'chocolate', null, 'Chocolate');
  80. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'butterscotch', null, 'Butterscotch');
  81. $checkbox[] = &HTML_QuickForm::createElement('checkbox', 'vanilla', null, 'Vanilla');
  82. $form->addGroup($checkbox, 'flavor', 'What types of ice cream do you like?', array('&nbsp;', '<br />'));
  83.  
  84.  
  85. $form->addElement('header', null, 'Submit the form');
  86. $submit[] =& $form->createElement('button', null, 'Upload', array('onClick'=>'DoUpload();'));
  87. $form->addGroup($submit, null, null, '&nbsp;', false);
  88.  
  89. $form->applyFilter('__ALL__', 'trim');
  90.  
  91. if ($form->validate()) {
  92.     // Form is validated, then processes the data
  93.     $form->freeze();
  94.     $form->process('myProcess', true);
  95.     echo '<p>&lt;&lt; <a target="_top" href="../index.html">Back examples TOC</a></p>';
  96.  
  97. } elseif (isset($_GET['ID'])) {
  98.     $destination = './uploads/';
  99.     $fp = fopen($destination . $_GET['ID'],'w',false);
  100.     fwrite($fp, 'error');
  101.     fclose($fp);
  102. }
  103. $form->display();
  104. ?>
  105. </body>
  106. </html>

The progress meter in indeterminate mode:

  1. <?php
  2. /**
  3.  * Progress meter is running in indeterminate mode while a file upload operation.
  4.  * This example may work with HTML_Progress 1.1
  5.  * but version 1.2.0 or better allows more easy facilities.
  6.  *
  7.  * @version    $Id: vbar.php,v 1.2 2005/07/25 11:43:56 farell Exp $
  8.  * @author     Laurent Laville <pear@laurent-laville.org>
  9.  * @package    HTML_Progress
  10.  * @subpackage Examples
  11.  */
  12.  
  13. require_once 'HTML/Progress.php';
  14.  
  15. function _methodExists($name)
  16. {
  17.     if (substr(PHP_VERSION,0,1) < '5') {
  18.         $n = strtolower($name);
  19.     } else {
  20.         $n = $name;
  21.     }
  22.     if (in_array($n, get_class_methods('HTML_Progress'))) {
  23.         return true;
  24.     }
  25.     return false;
  26. }
  27.  
  28. /*
  29.     User callback called pending progress meter is running, comes with version 1.2.0RC3
  30.  */
  31. function myFunctionHandler($progressValue, &$obj)
  32. {
  33.     global $version;
  34.     global $stop;
  35.     $semaphore = './uploads/'.$_GET['ID'];
  36.  
  37.     if (file_exists($semaphore)) {
  38.         $stop = file_get_contents($semaphore);
  39.         $obj->setValue(100);
  40.         $obj->setIndeterminate(false);
  41.         $obj->display();
  42.         unlink($semaphore);
  43.     }
  44.  
  45.     // sleep a bit ...
  46.     if ($version > 1.1) {
  47.         $obj->sleep();
  48.     } else {
  49.         for ($i=0; $i<($obj->_anim_speed*1000); $i++) { }
  50.     }
  51. }
  52.  
  53. /*
  54.     Which version of html_progress: (stable)1.1 or (beta)1.2.0 RC1, RC2 or RC3
  55.  */
  56. $version = _methodExists('run') ? 1.2 : 1.1;
  57.  
  58. $progress = new HTML_Progress(HTML_PROGRESS_BAR_VERTICAL);
  59. $progress->setIncrement(5);
  60. $progress->setAnimSpeed(100);
  61. $progress->setIndeterminate(true);     // progress bar run in indeterminate mode
  62. $progress->setStringPainted(true);     // get space for the string
  63. $progress->setString("");              // but don't paint it
  64. if ($version > 1.1) {
  65.     // set a progress handler required at least version 1.2.0RC3
  66.     $progress->setProgressHandler('myFunctionHandler');
  67. }
  68. $ui = & $progress->getUI();
  69. $ui->setCellCount(20);
  70. $ui->setBorderAttributes('width=1 color=#000000');
  71. $ui->setCellAttributes(array(
  72.     'active-color' => '#970038',
  73.     'inactive-color' => '#FFDDAA',
  74.     'width' => 20,
  75.     'height' => 20
  76. ));
  77. ?>
  78. <!DOCTYPE html
  79.     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  80.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  81.  
  82. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  83. <head>
  84. <style type="text/css">
  85. <!--
  86. body {
  87.     background-color: White;
  88.     color: red;
  89.     font-family: Verdana, Arial;
  90.     font-size: 10px;
  91. }
  92. <?php echo $progress->getStyle(); ?>
  93. // -->
  94. </style>
  95. <script type="text/javascript">
  96. <!--
  97. <?php echo $progress->getScript(); ?>
  98. //-->
  99. </script>
  100. </head>
  101. <body>
  102.  
  103. <?php
  104. echo $progress->toHtml();
  105.  
  106. if (isset($_GET['ID'])) {
  107.  
  108.     if ($version > 1.1) {
  109.         $progress->run();    // run method is born on version 1.2.0RC3
  110.     } else {
  111.         // do the same as run() method
  112.         do {
  113.             $progress->display();
  114.             myFunctionHandler($progress->getValue(), $progress);
  115.             if ($progress->getPercentComplete() == 1) {
  116.                 if ($progress->isIndeterminate()) {
  117.                     $progress->setValue(0);
  118.                 } else {
  119.                     break;
  120.                 }
  121.             }
  122.             $progress->incValue();
  123.         } while(1);
  124.     }
  125.     if ($stop == 'error') {
  126.         echo '<b>File was not uploaded !</b>';
  127.     } else {
  128.         echo '<b>Upload Complete...</b>';
  129.     }
  130. }
  131. ?>
  132.  
  133. </body>
  134. </html>

[Top]