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

Source for file observer.php

Documentation is available at observer.php

  1. <?php
  2. /**
  3.  * Net FTP Observer example to use with HTML_Progress package
  4.  * (PHP 4 >= PHP 4.3.0)
  5.  *
  6.  * @version    1.3
  7.  * @author     Laurent Laville <pear@laurent-laville.org>
  8.  * @author     Tobias Schlitt <toby@php.net>
  9.  * @link       http://pear.php.net/package/HTML_Progress
  10.  */
  11. require_once 'Net/FTP.php';
  12. require_once 'Net/FTP/Observer.php';
  13. require_once 'HTML/Progress.php';
  14.  
  15. /**
  16.  * Initializing test variables (required!)
  17.  */
  18. $ftp = array(
  19.     'host' => '',
  20.     'port' => 21,
  21.     'user' => '',
  22.     'pass' => ''
  23. );    
  24.  
  25. $dest 'tmp';                   // this directory must exists in your ftp server !
  26. $overwrite = true;               // overwrite all existing files on the ftp server
  27. $files = array(
  28.     'HTML_Progress-1.2.0.tgz',
  29.     'php4ever.png'               // initializing contents (required!) file(s) must exists
  30. );                               // file(s) to upload
  31.  
  32.  
  33. //
  34. // 1. Defines the FTP/Progress Observer 
  35. //
  36. {
  37.     var $progress;
  38.  
  39.     function Observer_ProgressUpload(&$progress)
  40.     {
  41.         /* Call the base class constructor. */
  42.         parent::Net_FTP_Observer();
  43.  
  44.         /**
  45.            Configure the observer:
  46.            
  47.            Be sure to have an indeterminate progress meter when
  48.            @link http://www.php.net/manual/en/function.ftp-nb-put.php
  49.            stores a file on the FTP server (non-blocking)
  50.          */
  51.         $this->progress =$progress;
  52.         $this->progress->setIndeterminate(true);
  53.     }
  54.  
  55.     function notify($event)
  56.     {
  57.         $this->progress->display();
  58.         $this->progress->sleep();
  59.                  
  60.         if ($this->progress->getPercentComplete(== 1{
  61.             $this->progress->setValue(0);
  62.         else {
  63.             $this->progress->incValue();
  64.         }
  65.     }
  66. }
  67.  
  68. //
  69. // 2. defines the progress meter 
  70. //
  71. $meter = new HTML_Progress();
  72. $ui $meter->getUI();
  73. $ui->setProgressAttributes(array(
  74.     'background-color' => '#e0e0e0'
  75. ));        
  76. $ui->setStringAttributes(array(
  77.     'color'  => '#996',
  78.     'background-color' => '#CCCC99'
  79. ));        
  80. $ui->setCellAttributes(array(
  81.     'active-color' => '#996'
  82. ));
  83.  
  84. $meter->setAnimSpeed(200);
  85. $meter->setIncrement(10);
  86. $meter->setStringPainted(true);     // get space for the string
  87. $meter->setString("");              // but don't paint it
  88. $meter->setIndeterminate(true);     // progress meter start in indeterminate mode
  89. ?>
  90. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  91.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  92. <html xmlns="http://www.w3.org/1999/xhtml">
  93. <head>
  94. <title>FTP/Progress Observer example</title>
  95. <style type="text/css">
  96. <!--
  97. body {
  98.     background-color: #CCCC99;
  99.     color: #996;
  100.     font-family: Verdana, Arial;
  101. }
  102.  
  103. <?php echo $meter->getStyle()?>
  104. // -->
  105. </style>
  106. <script type="text/javascript">
  107. <!--
  108. <?php echo $meter->getScript()?>
  109. //-->
  110. </script>
  111. </head>
  112. <body>
  113.  
  114. <?php 
  115. echo $meter->toHtml();
  116. @set_time_limit(0);  // unlimited time operation (removed 30s default restriction)
  117.  
  118. $f = new Net_FTP();
  119.  
  120. //
  121. // 3. connect to the FTP server 
  122. //
  123. $ret $f->connect($ftp['host']$ftp['port']);
  124. if (PEAR::isError($ret)) {
  125.     die($ret->getMessage());
  126. }
  127. printf('connected at <b>%s</b><br />'$ftp['host']);
  128.  
  129. //
  130. // 4. login to the FTP server as a well-known user
  131. //
  132. $ret $f->login($ftp['user']$ftp['pass']);
  133. if (PEAR::isError($ret)) {
  134.     $f->disconnect();
  135.     die($ret->getMessage());
  136. }
  137. printf('login as <b>%s</b><br />'$ftp['user']);
  138.  
  139. //
  140. // 5. changes directory to final destination for upload operation
  141. //
  142. $ret $f->cd($dest);
  143. if (PEAR::isError($ret)) {
  144.     $f->disconnect();
  145.     die($ret->getMessage());
  146. }
  147.  
  148. //
  149. // 6. attachs an instance of the FTP/Progress subclass observer
  150. //
  151. $observer = new Observer_ProgressUpload($meter);
  152. $ok $f->attach($observer);
  153. if (!$ok{
  154.     die('cannot attach a FTP Observer');
  155. }
  156.  
  157. //
  158. // 7. moves files on the FTP server
  159. //
  160. foreach($files as $file{
  161.     $ret $f->put($filebasename($file)$overwrite);
  162.     if (PEAR::isError($ret)) {
  163.         if (($ret->getCode(== NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDENand (!$overwrite)) {
  164.             printf('%s <br />'$ret->getMessage());
  165.             continue;  // it is just a warning when \$overwrite variable is set to false
  166.         }
  167.         die($ret->getMessage());
  168.     }
  169.     printf('<b>%s</b> transfer completed <br />'basename($file));
  170. }
  171. $f->detach($observer);
  172.  
  173. //
  174. // 8. checks if files are really on the FTP server
  175. //
  176. $ret $f->ls(nullNET_FTP_RAWLIST);
  177. if (PEAR::isError($ret)) {
  178.     $f->disconnect();
  179.     die($ret->getMessage());
  180. }
  181. print '<pre>';
  182. var_dump($ret);
  183. print '</pre>';
  184.  
  185. //
  186. // 9. says goodbye to the FTP server !
  187. //
  188. $f->disconnect();
  189. echo 'Done!';
  190. ?>

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