1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Net_FTP observer example.
*
* Net FTP Observer example to use with HTML_Progress package
* (PHP 4 >= PHP 4.3.0)
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Networking
* @package FTP
* @author Tobias Schlitt <toby@php.net>
* @author Laurent Laville <pear@laurent-laville.org>
* @copyright 1997-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id$
* @link http://pear.php.net/package/Net_FTP
* @link http://pear.php.net/package/HTML_Progress
* @since File available since Release 1.3.0
*/
require_once 'Net/FTP.php';
require_once 'Net/FTP/Observer.php';
require_once 'HTML/Progress.php';
/**
* Initializing test variables (required!)
*/
$ftp = array(
'host' => '',
'port' => 21,
'user' => '',
'pass' => ''
);
$dest = 'tmp'; // this directory must exists in your ftp server !
$overwrite = true; // overwrite all existing files on the ftp server
$files = array(
'HTML_Progress-1.2.0.tgz',
'php4ever.png' // initializing contents (required!) file(s) must
); // exists file(s) to upload
/**
* 1. Defines the FTP/Progress Observer
*
* @category Networking
* @package FTP
* @author Tobias Schlitt <toby@php.net>
* @copyright 1997-2008 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: 1.4.0
* @link http://pear.php.net/package/Net_FTP
* @since 0.0.1
* @access public
*/
class Observer_ProgressUpload extends Net_FTP_Observer
{
var $progress;
/**
* Constructor for the upload observer
*
* @param HTML_Progress &$progress Progress bar
*
* @return void
*/
function __construct(&$progress)
{
/* Call the base class constructor. */
parent::__construct();
/**
Configure the observer:
Be sure to have an indeterminate progress meter when
@link http://www.php.net/manual/en/function.ftp-nb-put.php
stores a file on the FTP server (non-blocking)
*/
$this->progress =& $progress;
$this->progress->setIndeterminate(true);
}
/**
* Notification method
*
* @param mixed $event Variable describing occured event
*
* @return void
*/
function notify($event)
{
$this->progress->display();
$this->progress->sleep();
if ($this->progress->getPercentComplete() == 1) {
$this->progress->setValue(0);
} else {
$this->progress->incValue();
}
}
}
//
// 2. defines the progress meter
//
$meter = new HTML_Progress();
$ui = &$meter->getUI();
$ui->setProgressAttributes(array(
'background-color' => '#e0e0e0'
));
$ui->setStringAttributes(array(
'color' => '#996',
'background-color' => '#CCCC99'
));
$ui->setCellAttributes(array(
'active-color' => '#996'
));
$meter->setAnimSpeed(200);
$meter->setIncrement(10);
$meter->setStringPainted(true); // get space for the string
$meter->setString(""); // but don't paint it
$meter->setIndeterminate(true); // progress meter start in indeterminate mode
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FTP/Progress Observer example</title>
<style type="text/css">
<!--
body {
background-color: #CCCC99;
color: #996;
font-family: Verdana, Arial;
}
<?php echo $meter->getStyle(); ?>
// -->
</style>
<script type="text/javascript">
<!--
<?php echo $meter->getScript(); ?>
//-->
</script>
</head>
<body>
<?php
echo $meter->toHtml();
@set_time_limit(0); // unlimited time operation (removed 30s default restriction)
$f = new Net_FTP();
//
// 3. connect to the FTP server
//
$ret = $f->connect($ftp['host'], $ftp['port']);
if (PEAR::isError($ret)) {
die($ret->getMessage());
}
printf('connected at <b>%s</b><br />', $ftp['host']);
//
// 4. login to the FTP server as a well-known user
//
$ret = $f->login($ftp['user'], $ftp['pass']);
if (PEAR::isError($ret)) {
$f->disconnect();
die($ret->getMessage());
}
printf('login as <b>%s</b><br />', $ftp['user']);
//
// 5. changes directory to final destination for upload operation
//
$ret = $f->cd($dest);
if (PEAR::isError($ret)) {
$f->disconnect();
die($ret->getMessage());
}
//
// 6. attachs an instance of the FTP/Progress subclass observer
//
$observer = new Observer_ProgressUpload($meter);
$ok = $f->attach($observer);
if (!$ok) {
die('cannot attach a FTP Observer');
}
//
// 7. moves files on the FTP server
//
foreach ($files as $file) {
$ret = $f->put($file, basename($file), $overwrite);
if (PEAR::isError($ret)) {
if ($ret->getCode() == NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN
&& !$overwrite) {
printf('%s <br />', $ret->getMessage());
continue; // it is just a warning when \$overwrite variable is false
}
die($ret->getMessage());
}
printf('<b>%s</b> transfer completed <br />', basename($file));
}
$f->detach($observer);
//
// 8. checks if files are really on the FTP server
//
$ret = $f->ls(null, NET_FTP_RAWLIST);
if (PEAR::isError($ret)) {
$f->disconnect();
die($ret->getMessage());
}
print '<pre>';
var_dump($ret);
print '</pre>';
//
// 9. says goodbye to the FTP server !
//
$f->disconnect();
echo 'Done!';
?>
|