20
Apr/11
0

FTP Client für PHP

(iframe AG, S-Node)

Auf der Suche nach einer geeigneten Lösung für den Datenaustausch zwischen einer E-Commerce Lösung, welche in PHP geschrieben ist, und einem Warenbewirtschaftungssystem.

Da ich eine Lösung erarbeiten sollte, die jederzeit aufrufbar sein soll, mit xml Dateien als Inhalt und Bildern umgehen sollte, habe wir uns darauf geeinigt einen FTP Server als Schnittstelle zu verwenden. Derjenige des Warenbewirtschaftungssystem lädt die Dateien da hoch und ich runter, und importiere diese dann in den Shop. Dafür habe ich eine Klasse geschrieben, welche ganz einfach ganze Verzeichnisstrukturen von einem FTP Server herunterladen kann.

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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
<?php
 
/**
* ftp.class.php
*
* Distributed under the GNU Lesser General Public License (LGPL v3)
* (http://www.gnu.org/licenses/lgpl.html)
* This program is distributed in the hope that it will be useful -
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @author Dominik Zogg <dominik.zogg@gmail.com
* @copyright Copyright (c) 2011, iframe AG
*
*/
 
class ftp {
/**
* @var array connectioninfo
*/
var $_connectioninfo = array(
"server" => "",
"username" => "",
"password" => "",
"encoding" => "",
"port" => 0,
"timeout" => 0,
);
/**
* @var null connection
*/
var $_connection = null;
/**
* construct
*
* @param string $server server must be a valid domain or ip
* @param string $username username of the ftp user
* @param string $password password of the ftp user
* @param int $port port of the ftp server
* @param int $timeout connection timeout time in seconds
*/
public function __construct($server, $username, $password, $encoding = "UTF-8", $port = 21, $timeout = 120) {
if(!preg_match("/^([a-zA-Z0-9\-\_\.\:]*)$/", trim($server))) {
throw new Exception("Please enter a valid Domain or IP!");
}
if(!is_int($port)) {
throw new Exception("Port must be a number!");
}
if(!is_int($timeout)) {
throw new Exception("Timeout must be a number!");
}
$this->_connectioninfo['server'] = trim($server);
$this->_connectioninfo['username'] = trim($username);
$this->_connectioninfo['password'] = trim($password);
$this->_connectioninfo['encoding'] = trim($encoding);
$this->_connectioninfo['port'] = $port;
$this->_connectioninfo['timeout'] = $timeout;
$this->_connect();
}
/**
* destruct
*/
public function __destruct() {
@ftp_close($this->_connection);
}
/**
* _connect
*/
private function _connect() {
if($this->_connection = @ftp_connect($this->_connectioninfo['server'], $this->_connectioninfo['port'], $this->_connectioninfo['timeout'])) {
if(!@ftp_login($this->_connection, $this->_connectioninfo['username'], $this->_connectioninfo['password'])) {
throw new Exception("Can't login to ftp server!");
}
if(!@ftp_pasv($this->_connection, true)) {
throw new Exception("Can't force passiv mode!");
}
}
else {
throw new Exception("Can't login to ftp server!");
}
}
/**
* filelist
*
* @param string $path path must be a valid unix path
* @return array filelist, each file is an array element
*/
public function filelist($path = "") {
$return = array();
$elements = $this->_rawlist($path);
if(is_array($elements)) {
foreach($elements as $elementkey => $element) {
if($element['type'] !== 1) {
$return[$elementkey] = $element;
}
}
}
return($return);
}
/**
* folderlist
*
* @param string $path path must be a valid unix path
* @return array folderlist, each file is an array element
*/
public function folderlist($path = "") {
$return = array();
$elements = $this->_rawlist($path);
if(is_array($elements)) {
foreach($elements as $elementkey => $element) {
if($element['type'] === 1) {
$return[$elementkey] = $element;
}
}
}
return($return);
}
/**
* downloadfile
*
* @param string $remotepath filepath on remotehost
* @param string $localpath filepath on localhost
*/
public function downloadfile($remotepath, $localpath, $overwrite = false) {
$this->_resetremotepath();
if(@is_file($localpath) && !$overwrite) {
throw new Exception("Localfile '{$localpath}' allready exists! Force overwrite by calling with option overwrite = true!");
}
if(!@ftp_get($this->_connection, $localpath, iconv("UTF-8", $this->_connectioninfo['encoding'], $remotepath), FTP_BINARY)) {
//reconnect if download failed
$this->_connect();
if(!@ftp_get($this->_connection, $localpath, iconv("UTF-8", $this->_connectioninfo['encoding'], $remotepath), FTP_BINARY)) {
throw new Exception("Remotefile is not downloadable: {$remotepath}");
}
}
}
/**
* downloadfolder
*
* @param string $remotepath filepath on remotehost
* @param string $localpath filepath on localhost
*/
public function downloadfolder($remotepath, $localpath, $overwrite = false) {
$this->_resetremotepath@import url('https://gist.github.com/assets/embed-0af287a4b5c981db301049e56f06e5d3.css');

Zurück

Einen Kommentar schreiben