Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Transfer files from FTP using PHP functions. This class also
- * implements a retry mechanism (you can set either a finite or
- * infinite number of attempts).
- *
- * Copyright (C) 2016 - by Alexandru Rusu
- * @author Alexandru Rusu <arusu@bfproject.ro>
- * @license MIT License
- */
- class FtpRetryTransferHelper {
- /** @var string */
- private $hostname = "";
- /** @var integer */
- private $hostport = 21;
- /** @var string */
- private $username = "";
- /** @var string */
- private $password = "";
- /** @var integer */
- private $timeout = 30;
- /** @var integer */
- private $maxRetry = 3;
- /** @var integer */
- private $crtRetry = 0;
- /** @var resource */
- private $connection = NULL;
- /** @var NULL|string */
- private $lstError = NULL;
- /**
- * Class constructor
- *
- * @param string $host Remote FTP server hostname
- * @param string $user Remote FTP server user
- * @param string $pass Remote FTP server password
- * @param string $port Remote FTP server port
- * @param string $timeout Connection timeout
- * @param string $maxRetry Max number of retries (use null or 0 for unlimited)
- */
- public function __construct($host, $user, $pass, $port = NULL, $timeout = NULL, $maxRetry = NULL) {
- $this->hostname = $host;
- $this->username = $user;
- $this->password = $pass;
- if (!empty($port) && !empty(intval($port))) $this->hostport = $port;
- if (!empty($timeput) && !empty(intval($timeout))) $this->timeout = $timeout;
- if (!empty($maxRetry) && !empty(intval($maxRetry))) $this->maxRetry = $maxRetry;
- // Connect to host
- $this->connection = ftp_connect($this->hostname, $this->hostport, $this->timeout);
- if (!$this->connection)
- throw new Exception("Cannot connect to FTP host.");
- // Login to the user account
- if (!ftp_login($this->connection, $this->username, $this->password))
- thrown new Exception("Cannot authenticate to FTP host.");
- // Set passive mode for transfer
- @ftp_pasv($this->connection, TRUE);
- }
- /**
- * Class destructor
- */
- public function __destruct() {
- // Make sure to close the connection
- ftp_close($this->connection);
- }
- /**
- * Transfer a file from FTP
- *
- * @param string $remotePath Remote FTP host path of the file to be transfered
- * @param string $localPath Local path where the file should be saved
- * @param const $transferMode (optional) Transfer mode, can be FTP_BINARY or FTP_ASCII
- *
- * @return bool Result of the operation
- */
- public function downloadFile($remotePath, $localPath, $transferMode = FTP_BINARY) {
- $transfered = FALSE;
- $this->lstError = NULL;
- $this->crtRetry = 0;
- while (!$transfered && !empty($this->lstError) && (empty($this->maxRetry) || $this->crtRetry < $this->maxRetry)) {
- ++$this->crtRetry;
- $transfered = $this->_resumeDownload($remotePath, $localPath, $transferMode);
- }
- return $transfered;
- }
- /**
- * Get last error
- *
- * @return mixed Last transfer error or FALSE if transfer was successful
- */
- public function getLastError() {
- return empty($this->lstError) ? FALSE : $this->lstError;
- }
- /**
- * Internal method to start or resume a download
- *
- * @param string $remotePath Remote FTP host path of the file to be transfered
- * @param string $localPath Local path where the file should be saved
- * @param const $transferMode (optional) Transfer mode, can be FTP_BINARY or FTP_ASCII
- *
- * @return bool Result of the operation
- */
- private function _resumeDownload($remotePath, $localPath, $transferMode = FTP_BINARY) {
- $transfered = FALSE;
- $this->lstError = NULL;
- try {
- $resumePosition = file_exists($localPath) ? filesize($localPath) : 0;
- $transfered = ftp_get($this->connection, $localPath, $remotePath, $transferMode, $resumePosition);
- if (!$transfered) {
- $this->lstError = "Failed transfering file on retry " . ($this->crtRetry + 1) . " of " . $this->maxRetry;
- }
- } catch (Exception $exp) {
- $transfered = FALSE;
- $this->lstError = $exp->getMessage();
- }
- return $transfered;
- }
- }
- /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement