Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <?php
  2. /**
  3. * Transfer files from FTP using PHP functions. This class also
  4. * implements a retry mechanism (you can set either a finite or
  5. * infinite number of attempts).
  6. *
  7. * Copyright (C) 2016 - by Alexandru Rusu
  8. * @author Alexandru Rusu <arusu@bfproject.ro>
  9. * @license MIT License
  10. */
  11. class FtpRetryTransferHelper {
  12.  
  13. /** @var string */
  14. private $hostname = "";
  15. /** @var integer */
  16. private $hostport = 21;
  17. /** @var string */
  18. private $username = "";
  19. /** @var string */
  20. private $password = "";
  21.  
  22. /** @var integer */
  23. private $timeout = 30;
  24. /** @var integer */
  25. private $maxRetry = 3;
  26. /** @var integer */
  27. private $crtRetry = 0;
  28.  
  29. /** @var resource */
  30. private $connection = NULL;
  31. /** @var NULL|string */
  32. private $lstError = NULL;
  33.  
  34. /**
  35. * Class constructor
  36. *
  37. * @param string $host Remote FTP server hostname
  38. * @param string $user Remote FTP server user
  39. * @param string $pass Remote FTP server password
  40. * @param string $port Remote FTP server port
  41. * @param string $timeout Connection timeout
  42. * @param string $maxRetry Max number of retries (use null or 0 for unlimited)
  43. */
  44. public function __construct($host, $user, $pass, $port = NULL, $timeout = NULL, $maxRetry = NULL) {
  45.  
  46. $this->hostname = $host;
  47. $this->username = $user;
  48. $this->password = $pass;
  49.  
  50. if (!empty($port) && !empty(intval($port))) $this->hostport = $port;
  51. if (!empty($timeput) && !empty(intval($timeout))) $this->timeout = $timeout;
  52. if (!empty($maxRetry) && !empty(intval($maxRetry))) $this->maxRetry = $maxRetry;
  53.  
  54. // Connect to host
  55. $this->connection = ftp_connect($this->hostname, $this->hostport, $this->timeout);
  56. if (!$this->connection)
  57. throw new Exception("Cannot connect to FTP host.");
  58.  
  59. // Login to the user account
  60. if (!ftp_login($this->connection, $this->username, $this->password))
  61. thrown new Exception("Cannot authenticate to FTP host.");
  62.  
  63. // Set passive mode for transfer
  64. @ftp_pasv($this->connection, TRUE);
  65. }
  66.  
  67. /**
  68. * Class destructor
  69. */
  70. public function __destruct() {
  71. // Make sure to close the connection
  72. ftp_close($this->connection);
  73. }
  74.  
  75. /**
  76. * Transfer a file from FTP
  77. *
  78. * @param string $remotePath Remote FTP host path of the file to be transfered
  79. * @param string $localPath Local path where the file should be saved
  80. * @param const $transferMode (optional) Transfer mode, can be FTP_BINARY or FTP_ASCII
  81. *
  82. * @return bool Result of the operation
  83. */
  84. public function downloadFile($remotePath, $localPath, $transferMode = FTP_BINARY) {
  85. $transfered = FALSE;
  86. $this->lstError = NULL;
  87. $this->crtRetry = 0;
  88. while (!$transfered && !empty($this->lstError) && (empty($this->maxRetry) || $this->crtRetry < $this->maxRetry)) {
  89. ++$this->crtRetry;
  90. $transfered = $this->_resumeDownload($remotePath, $localPath, $transferMode);
  91. }
  92. return $transfered;
  93. }
  94.  
  95. /**
  96. * Get last error
  97. *
  98. * @return mixed Last transfer error or FALSE if transfer was successful
  99. */
  100. public function getLastError() {
  101. return empty($this->lstError) ? FALSE : $this->lstError;
  102. }
  103.  
  104. /**
  105. * Internal method to start or resume a download
  106. *
  107. * @param string $remotePath Remote FTP host path of the file to be transfered
  108. * @param string $localPath Local path where the file should be saved
  109. * @param const $transferMode (optional) Transfer mode, can be FTP_BINARY or FTP_ASCII
  110. *
  111. * @return bool Result of the operation
  112. */
  113. private function _resumeDownload($remotePath, $localPath, $transferMode = FTP_BINARY) {
  114. $transfered = FALSE;
  115. $this->lstError = NULL;
  116. try {
  117. $resumePosition = file_exists($localPath) ? filesize($localPath) : 0;
  118. $transfered = ftp_get($this->connection, $localPath, $remotePath, $transferMode, $resumePosition);
  119. if (!$transfered) {
  120. $this->lstError = "Failed transfering file on retry " . ($this->crtRetry + 1) . " of " . $this->maxRetry;
  121. }
  122. } catch (Exception $exp) {
  123. $transfered = FALSE;
  124. $this->lstError = $exp->getMessage();
  125. }
  126. return $transfered;
  127. }
  128. }
  129.  
  130. /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement