HTML_Progress
[ class tree: HTML_Progress ] [ index: HTML_Progress ] [ all elements ]

Source for file ftpupload.php

Documentation is available at ftpupload.php

  1. <?php
  2. @include '../include_path.php';
  3. /**
  4.  * FTP file Upload
  5.  * This example shows how to upload file on a ftp server,
  6.  * that may be different than web server.
  7.  *
  8.  * @version    $Id: ftpupload.php,v 1.1 2004/07/02 17:51:34 farell Exp $
  9.  * @author     Laurent Laville <pear@laurent-laville.org>
  10.  * @package    HTML_Progress
  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'null3'client');
  114.  
  115. $form->addElement('text''ftpdir''FTP directory:');
  116.  
  117. $radio[&HTML_QuickForm::createElement('radio'nullnull'Yes''1');
  118. $radio[&HTML_QuickForm::createElement('radio'nullnull'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($submitnullnull'&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>

Documentation generated on Mon, 11 Mar 2019 13:52:37 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.