Examples TOCexamples

Web-FTP ProgressBar uploader

$Date: 2004/08/10 18:13:10 $

 Table of contents

Introduction

This example requires :


This example allows you to upload files from a web browser to a ftp server (may be different and on another host that your web server).

PEAR::HTML_QuickForm package is used to make and manage the form that will send file to your web server (left frame), while HTML_Progress is only used to display a horizontal progress meter running in indeterminate mode (right 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 = 10 (to make progress bar animation smoothest in indeterminate mode)
HTML_Progress::setIncrement()
speed = 100 (to make progress bar animation smoothest)
HTML_Progress::setAnimSpeed()
background-color = #e0e0e0
HTML_Progress_UI::setProgressAttributes()
color            = #996
background-color = #CCCC99
HTML_Progress_UI::setStringAttributes()
active-color     = #996
HTML_Progress_UI::setCellAttributes()

[Top]

 Output

Screenshot

[Top]

 PHP source syntax highlight

The form file selection:

  1. <?php
  2. /**
  3.  * FTP file Upload
  4.  * This example shows how to upload file on a ftp server,
  5.  * that may be different than web server.
  6.  *
  7.  * @version    $Id: ftpupload.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/QuickForm.php';
  14. require_once 'Net/FTP.php';
  15.  
  16. function myProcess($values)
  17. {
  18.     global $form;
  19.     $destination = './uploads/';
  20.  
  21.     // Account FTP on remote server, directory destination, and allows Y/N file overwriting
  22.     $ftp = array(
  23.         'user' => $values['ftpaccount']['U'],
  24.         'pass' => $values['ftpaccount']['P'],
  25.         'host' => $values['ftpaccount']['H'],
  26.         'dest' => $values['ftpdir'],             // this directory must exists in your ftp server !
  27.         'overwrite' => (bool)$values['overwrite']
  28.     );
  29.  
  30.     $result = 'done';
  31.     $file =& $form->getElement('tstUpload');
  32.  
  33.     if ($file->isUploadedFile()) {
  34.  
  35.         $_ftp = new Net_FTP($ftp['host']);
  36.  
  37.         $ret = $_ftp->connect();
  38.         if (PEAR::isError($ret)) {
  39.             $result = $ret->getMessage();                  // NET_FTP_ERR_CONNECT_FAILED
  40.         } else {
  41.             $ret = $_ftp->login($ftp['user'], $ftp['pass']);
  42.             if (PEAR::isError($ret)) {
  43.                 $result = $ret->getMessage();              // NET_FTP_ERR_LOGIN_FAILED
  44.             } else {
  45.                 $_ftp->setPassive();
  46.  
  47.                 $ret = $_ftp->cd($ftp['dest']);
  48.                 if (PEAR::isError($ret)) {
  49.                     $result = $ret->getMessage();          // NET_FTP_ERR_DIRCHANGE_FAILED
  50.                 } else {
  51.                     $fval = $file->getValue();
  52.  
  53.                     $ret = $_ftp->put($fval['tmp_name'], $fval['name'], $ftp['overwrite']);
  54.                     if (PEAR::isError($ret)) {
  55.                         $result = $ret->getMessage();      // NET_FTP_ERR_UPLOADFILE_FAILED
  56.                     }
  57.                 }
  58.             }
  59.             $ret = $_ftp->disconnect();
  60.             if (PEAR::isError($ret)) {
  61.                 $result = $ret->getMessage();              // NET_FTP_ERR_DISCONNECT_FAILED
  62.             }
  63.         }
  64.     }
  65.  
  66.     // write the semaphore to tell progress meter to stop
  67.     // in script 'progressbar.php'
  68.  
  69.     $semaphore = $destination . $_GET['ID'];
  70.     $fp = fopen($semaphore,'w',false);
  71.     fwrite($fp, $result);
  72.     fclose($fp);
  73. }
  74. ?>
  75. <html>
  76. <head>
  77. <script language="javascript">
  78. <!--
  79. function DoUpload() {
  80.   theUniqueID = (new Date()).getTime() % 1000000000;
  81.   parent.meter.window.location = "progressbar.php?ID=" + theUniqueID;
  82.   parent.files.selfref.action = "ftpupload.php?ID=" + theUniqueID;
  83.   parent.files.selfref.submit();
  84. }
  85. //-->
  86. </script>
  87. </head>
  88. <body>
  89.  
  90. <?php
  91.  
  92. $form =& new HTML_QuickForm('selfref');
  93.  
  94. $renderer =& $form->defaultRenderer();
  95. $renderer->setElementTemplate(<<<EOT
  96. <tr>
  97.     <td align="right" valign="top" nowrap="nowrap"><!-- BEGIN required --><span style="color: #ff0000">*</span><!-- END required --><b>{label}</b></td>
  98.     <td valign="top" align="left">
  99.         <!-- BEGIN error --><span style="color: #ff0000">{error}</span><br /><!-- END error -->{element}
  100.         <!-- BEGIN label_2 --><br/><span style="font-size: 80%">{label_2}</span><!-- END label_2 -->
  101.     </td>
  102. </tr>
  103.  
  104. EOT
  105. );
  106.  
  107. $form->addElement('header', null, 'Uploaded file rules');
  108.  
  109. $account['host'] = &HTML_QuickForm::createElement('text', 'H', 'host');
  110. $account['user'] = &HTML_QuickForm::createElement('text', 'U', 'user');
  111. $account['pass'] = &HTML_QuickForm::createElement('password', 'P', 'password');
  112. $form->addGroup($account, 'ftpaccount', 'FTP account:');
  113. $form->addGroupRule('ftpaccount', 'The FTP account is required', 'required', null, 3, 'client');
  114.  
  115. $form->addElement('text', 'ftpdir', 'FTP directory:');
  116.  
  117. $radio[] = &HTML_QuickForm::createElement('radio', null, null, 'Yes', '1');
  118. $radio[] = &HTML_QuickForm::createElement('radio', null, null, 'No',  '0');
  119. $form->addGroup($radio,  'overwrite', 'Overwrite existing files:');
  120. $form->addRule('overwrite', 'Check Yes or No', 'required', null, 'client');
  121.  
  122. $form->addElement('file', 'tstUpload', array('Upload file:', 'Rule types: \'uploadedfile\' \'upload_max_filesize\'='.ini_get('upload_max_filesize')));
  123. $form->addRule('tstUpload', 'Upload is required', 'uploadedfile');
  124.  
  125. $form->addElement('header', null, 'Submit the form');
  126. $submit[] =& $form->createElement('button', null, 'Upload', array('onClick'=>'DoUpload();'));
  127. $form->addGroup($submit, null, null, '&nbsp;', false);
  128.  
  129. $form->applyFilter('__ALL__', 'trim');
  130.  
  131. if ($form->validate()) {
  132.     // Form is validated, then processes the data
  133.     $form->freeze();
  134.     $form->process('myProcess', true);
  135.     echo '<p>&lt;&lt; <a target="_top" href="../index.html">Back examples TOC</a></p>';
  136. }
  137. $form->display();
  138. ?>
  139. </body>
  140. </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: progressbar.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/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();
  59. $progress->setIncrement(10);
  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->setProgressAttributes(array(
  70.     'background-color' => '#e0e0e0'
  71. ));
  72. $ui->setStringAttributes(array(
  73.     'color'  => '#996',
  74.     'background-color' => '#CCCC99'
  75. ));
  76. $ui->setCellAttributes(array(
  77.     'active-color' => '#996'
  78. ));
  79. ?>
  80. <html>
  81. <head>
  82. <style type="text/css">
  83. <!--
  84. body {
  85.     background-color: #CCCC99;
  86.     color: #996;
  87.     font-family: Verdana, Arial;
  88. }
  89. <?php echo $progress->getStyle(); ?>
  90. // -->
  91. </style>
  92. <script type="text/javascript">
  93. <!--
  94. <?php echo $progress->getScript(); ?>
  95. //-->
  96. </script>
  97. </head>
  98. <body>
  99.  
  100. <?php
  101. echo $progress->toHtml();
  102.  
  103. if (isset($_GET['ID'])) {
  104.  
  105.     if ($version > 1.1) {
  106.         $progress->run();    // run method is born on version 1.2.0RC3
  107.     } else {
  108.         // do the same as run() method
  109.         do {
  110.             $progress->display();
  111.             myFunctionHandler($progress->getValue(), $progress);
  112.             if ($progress->getPercentComplete() == 1) {
  113.                 if ($progress->isIndeterminate()) {
  114.                     $progress->setValue(0);
  115.                 } else {
  116.                     break;
  117.                 }
  118.             }
  119.             $progress->incValue();
  120.         } while(1);
  121.     }
  122.     if ($stop == 'done') {
  123.         echo '<b>Upload Complete...</b>';
  124.     } else {
  125.         echo '<b>File was not uploaded !</b>';
  126.         echo '<br/><font size="1">'.$stop.'</font>';
  127.     }
  128. }
  129. ?>
  130.  
  131. </body>
  132. </html>

[Top]