Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. <?php
  2. class ftpObj {
  3. private $ftpConn, $host, $hostPort, $user, $password, $proxyHost, $proxyPort;
  4. function __construct($host, $hostPort, $user, $password, $proxyHost = false, $proxyPort = false) {
  5. $this->host = $host;
  6. $this->hostPort = $hostPort;
  7. $this->user = $user;
  8. $this->password = $password;
  9. $this->proxyHost = $proxyHost;
  10. $this->proxyPort = $proxyPort;
  11.  
  12. $this->connect();
  13. }
  14.  
  15. function connect() {
  16. $connected = false;
  17. if ($this->proxyHost === false) {
  18. $this->ftpConn = ftp_connect($this->host,$this->hostPort);
  19. $connected = ftp_login($this->ftpConn, $this->user, $this->password);
  20. }
  21. else {
  22. $this->ftpConn = ftp_connect($this->proxyHost,$this->proxyPort);
  23. $connected = ftp_login($this->ftpConn, $this->user . '@' . $this->host . ':' . $this->hostPort, $this->password);
  24. }
  25. if (!$connected) die('failed to connect to FTP!');
  26. ftp_pasv($this->ftpConn, TRUE);
  27. ftp_set_option($this->ftpConn, FTP_TIMEOUT_SEC, 9999);
  28. }
  29.  
  30. function download($file, $fromPath, $toPath, $resume = 0) {
  31. $resumeFrom = 0;
  32. if ($resume > 0) {
  33. $this->connect();
  34. $resumeFrom = filesize($toPath . $file);
  35. }
  36. if ($resumeFrom == 0 && filesize($toPath . $file) > -1) unlink($toPath . $file);
  37. if (ftp_get($this->ftpConn, $toPath . $file, $fromPath . $file, FTP_BINARY, $resumeFrom)) {
  38. echo "successfully downloaded $file\n";
  39. } else {
  40. echo "failed downloading $file, retrying\n";
  41. $resume++;
  42. if ($resume > 10) {
  43. echo "failed downloading $file too many times for retry\n";
  44. } else $this->download($file, $fromPath, $toPath, $resume);
  45. }
  46. }
  47.  
  48. function upload($file, $fromPath, $toPath, $resume = 0) {
  49. $resumeFrom = 0;
  50. if ($resume > 0) {
  51. $this->connect();
  52. $resumeFrom = ftp_size($this->ftpConn, $toPath . $file);
  53. }
  54. if ($resumeFrom == 0 && ftp_size($this->ftpConn, $toPath . $file) > -1) ftp_delete($this->ftpConn, $toPath . $file);
  55. if (ftp_put($this->ftpConn, $toPath . $file, $fromPath . $file, FTP_BINARY, $resumeFrom)) {
  56. echo "successfully uploaded $file\n";
  57. } else {
  58. echo "failed uploading $file, retrying\n";
  59. $resume++;
  60. if ($resume > 10) {
  61. echo "failed uploading $file too many times for retry\n";
  62. } else $this->upload($file, $fromPath, $toPath, $resume);
  63. }
  64. }
  65.  
  66. function uncompress($path, $file) {
  67. $fh = gzopen($path . $file, 'rb');
  68. $outFh = fopen(str_replace('.gz', '', $path . $file), 'wb');
  69. if (!$fh) die("failed to create file: $file");
  70. while(!gzeof($fh)) fwrite($outFh, gzread($fh, 8192));
  71. fclose($outFh);
  72. gzclose($fh);
  73. unlink($path . $file);
  74. echo "successfully uncompressed $file\n";
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement