Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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