This class is for FTP upload / downloading in PHP. Has recursive capability so it can upload / download entire directory and sub-directory structures.
Credit goes to Kristian Feldsam – iKFSystems, in**@ik********.sk
FtpClassForRecursive.php download
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | class ftp { private $conn, $login_result, $logData, $ftpUser, $ftpPass, $ftpHost, $retry, $ftpPasv, $ftpMode, $verbose, $logPath, $createMask; // -------------------------------------------------------------------- /** * Construct method * * @param array keys[passive_mode(true|false)|transfer_mode(FTP_ASCII|FTP_BINARY)| reattempts(int)|log_path|verbose(true|false)|create_mask(default:0777)] * @return void */ function __construct($o) { $this->retry = (isset($o['reattempts'])) ? $o['reattempts'] : 3; $this->ftpPasv = (isset($o['passive_mode'])) ? $o['passive_mode'] : true; $this->ftpMode = (isset($o['transfer_mode'])) ? $o['transfer_mode'] : FTP_BINARY; $this->verbose = (isset($o['verbose'])) ? $o['verbose'] : false; $this->logPath = (isset($o['log_path'])) ? $o['log_path'] : false; $this->createMask = (isset($o['create_mask'])) ? $o['create_mask'] : 0777; } // -------------------------------------------------------------------- /** * Connection method * * @param string hostname * @param string username * @param string password * @return void */ public function conn($hostname, $username, $password) { $this->ftpUser = $username; $this->ftpPass = $password; $this->ftpHost = $hostname; $this->initConn(); } // -------------------------------------------------------------------- /** * Init connection method - connect to ftp server and set passive mode * * @return bool */ function initConn() { $this->conn = ftp_connect($this->ftpHost); $this->login_result = ftp_login($this->conn, $this->ftpUser, $this->ftpPass); if($this->conn && $this->login_result) { ftp_pasv($this->conn, $this->ftpPasv); return true; } return false; } // -------------------------------------------------------------------- /** * Put method - upload files(folders) to ftp server * * @param string path to destionation file/folder on ftp * @param string path to source file/folder on local disk * @param int only for identify reattempt, dont use this param * @return bool */ public function put($destinationFile, $sourceFile, $retry = 0) { if(file_exists($sourceFile)) { if(!$this->isDir($sourceFile, true)) { $this->createSubDirs($destinationFile); if(!ftp_put($this->conn, $destinationFile, $sourceFile, $this->ftpMode)) { $retry++; if($retry > $this->retry) { $this->logData('Error when uploading file: '.$sourceFile.' => '.$destinationFile, 'error'); return false; } if($this->verbose) echo 'Retry: '.$retry."\n"; $this->reconnect(); $this->put($destinationFile, $sourceFile, $retry); } else { $this->logData($sourceFile.' => '.$destinationFile, 'ok'); return true; } } else { $this->recursive($destinationFile, $sourceFile, 'put'); } } } // -------------------------------------------------------------------- /** * Get method - download files(folders) from ftp server * * @param string path to destionation file/folder on local disk * @param string path to source file/folder on ftp server * @param int only for identify reattempt, dont use this param * @return bool */ public function get($destinationFile, $sourceFile, $retry = 0) { if(!$this->isDir($sourceFile, false)) { if($this->verbose)echo $sourceFile.' => '.$destinationFile."\n"; $this->createSubDirs($destinationFile, false, true); if(!ftp_get($this->conn, $destinationFile, $sourceFile, $this->ftpMode)) { $retry++; if($retry > $this->retry) { $this->logData('Error when downloading file: '.$sourceFile.' => '.$destinationFile, 'error'); return false; } if($this->verbose) echo 'Retry: '.$retry."\n"; $this->reconnect(); $this->get($destinationFile, $sourceFile, $retry); } else { $this->logData($sourceFile.' => '.$destinationFile, 'ok'); return true; } } else { $this->recursive($destinationFile, $sourceFile, 'get'); } } // -------------------------------------------------------------------- /** * Make dir method - make folder on ftp server or local disk * * @param string path to destionation folder on ftp or local disk * @param bool true for local, false for ftp * @return bool */ public function makeDir($dir, $local = false) { if($local) { if(!file_exists($dir) && !is_dir($dir))return mkdir($dir, $this->createMask); else return true; } else { ftp_mkdir($this->conn,$dir); return ftp_chmod($this->conn, $this->createMask, $dir); } } // -------------------------------------------------------------------- /** * Cd up method - change working dir up * * @param bool true for local, false for ftp * @return bool */ public function cdUp($local) { return $local ? chdir('..') : ftp_cdup($this->conn); } // -------------------------------------------------------------------- /** * List contents of dir method - list all files in specified directory * * @param string path to destionation folder on ftp or local disk * @param bool true for local, false for ftp * @return bool */ public function listFiles($file, $local = false) { if(!$this->isDir($file, $local))return false; if($local) { return scandir($file); } else { if(!preg_match('/\//', $file)) { return ftp_nlist($this->conn, $file); }else { $dirs = explode('/', $file); foreach($dirs as $dir) { $this->changeDir($dir, $local); } $last = count($dirs)-1; $this->cdUp($local); $list = ftp_nlist($this->conn, $dirs[$last]); $i = 0; foreach($dirs as $dir) { if($i < $last) $this->cdUp($local); $i++; } return $list; } } } // -------------------------------------------------------------------- /** * Returns current working directory * * @param bool true for local, false for ftp * @return bool */ public function pwd($local = false) { return $local ? getcwd() : ftp_pwd($this->conn); } // -------------------------------------------------------------------- /** * Change current working directory * * @param string dir name * @param bool true for local, false for ftp * @return bool */ public function changeDir($dir, $local = false) { return $local ? chdir($dir) : @ftp_chdir($this->conn, $dir); } // -------------------------------------------------------------------- /** * Create subdirectories * * @param string path * @param bool * @param bool true for local, false for ftp * @param bool change current working directory back * @return void */ function createSubDirs($file, $last = false, $local = false, $chDirBack = true) { if(preg_match('/\//',$file)) { $origin = $this->pwd($local); if(!$last) $file = substr($file, 0, strrpos($file,'/')); $dirs = explode('/',$file); foreach($dirs as $dir) { if(!$this->isDir($dir, $local)) { $this->makeDir($dir, $local); $this->changeDir($dir, $local); } else { $this->changeDir($dir, $local); } } if($chDirBack) $this->changeDir($origin, $local); } } // -------------------------------------------------------------------- /** * Recursion * * @param string destionation file/folder * @param string source file/folder * @param string put or get * @return void */ function recursive($destinationFile, $sourceFile, $mode) { $local = ($mode == 'put') ? true : false; $list = $this->listFiles($sourceFile, $local); if($this->verbose) echo "\n".'Folder: '.$sourceFile."\n"; if($this->verbose) print_r($list); $x=0; $z=0; foreach($list as $file) { if($file == '.' || $file == '..')continue; $destFile = $destinationFile.'/'.$file; $srcFile = $sourceFile.'/'.$file; if($this->isDir($srcFile,$local)) { $this->recursive($destFile, $srcFile, $mode); } else { if($local) { $this->put($destFile, $srcFile); } else { $this->get($destFile, $srcFile); } } } } // -------------------------------------------------------------------- /** * Check if is dir * * @param string path to folder * @return bool */ public function isDir($dir, $local) { if($local) return is_dir($dir); if($this->changeDir($dir))return $this->cdUp(0); return false; } // -------------------------------------------------------------------- /** * Save log data to array * * @param string data * @param string type(error|ok) * @return void */ function logData($data, $type) { $this->logData[$type][] = $data; } // -------------------------------------------------------------------- /** * Get log data array * * @return array */ public function getLogData() { return $this->logData; } // -------------------------------------------------------------------- /** * Save log data to file * * @return void */ public function logDataToFiles() { if(!$this->logPath) return false; $ftp->makeDir($this->logPath, true); $log = $ftp->getLogData(); $sep = "\n".date('y-m-d H-i-s').' '; $logc = date('y-m-d H-i-s').' '.join($sep,$log['error'])."\n"; $this->addToFile($this->logPath.'/'.$config->name.'-error.log',$logc); $logc = date('y-m-d H-i-s').' '.join($sep,$log['ok'])."\n"; $this->addToFile($this->logPath.'/'.$config->name.'-ok.log',$logc); } // -------------------------------------------------------------------- /** * Reconnect method * * @return void */ public function reconnect() { $this->closeConn(); $this->initConn(); } // -------------------------------------------------------------------- /** * Close connection method * * @return void */ public function closeConn() { return ftp_close($this->conn); } // -------------------------------------------------------------------- /** * Write to file * * @param string path to file * @param string text * @param string fopen mode * @return void */ function addToFile($file, $ins, $mode = 'a') { $fp = fopen($file, $mode); fwrite($fp,$ins); fclose($fp); } // -------------------------------------------------------------------- /** * Destruct method - close connection and save log data to file * * @return void */ function __destruct() { $this->closeConn(); $this->logDataToFiles(); } } // END ftp class /* End of file ftp.php */ /* Location: ftp.php */ |