Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ftpObj {
- private $ftpConn, $host, $hostPort, $user, $password, $proxyHost, $proxyPort;
- function __construct($host, $hostPort, $user, $password, $proxyHost = false, $proxyPort = false) {
- $this->host = $host;
- $this->hostPort = $hostPort;
- $this->user = $user;
- $this->password = $password;
- $this->proxyHost = $proxyHost;
- $this->proxyPort = $proxyPort;
- $this->connect();
- }
- function connect() {
- $connected = false;
- if ($this->proxyHost === false) {
- $this->ftpConn = ftp_connect($this->host,$this->hostPort);
- $connected = ftp_login($this->ftpConn, $this->user, $this->password);
- }
- else {
- $this->ftpConn = ftp_connect($this->proxyHost,$this->proxyPort);
- $connected = ftp_login($this->ftpConn, $this->user . '@' . $this->host . ':' . $this->hostPort, $this->password);
- }
- if (!$connected) die('failed to connect to FTP!');
- ftp_pasv($this->ftpConn, TRUE);
- ftp_set_option($this->ftpConn, FTP_TIMEOUT_SEC, 9999);
- }
- function download($file, $fromPath, $toPath, $resume = 0) {
- $resumeFrom = 0;
- if ($resume > 0) {
- $this->connect();
- $resumeFrom = filesize($toPath . $file);
- }
- if ($resumeFrom == 0 && filesize($toPath . $file) > -1) unlink($toPath . $file);
- if (ftp_get($this->ftpConn, $toPath . $file, $fromPath . $file, FTP_BINARY, $resumeFrom)) {
- echo "successfully downloaded $file\n";
- } else {
- echo "failed downloading $file, retrying\n";
- $resume++;
- if ($resume > 10) {
- echo "failed downloading $file too many times for retry\n";
- } else $this->download($file, $fromPath, $toPath, $resume);
- }
- }
- function upload($file, $fromPath, $toPath, $resume = 0) {
- $resumeFrom = 0;
- if ($resume > 0) {
- $this->connect();
- $resumeFrom = ftp_size($this->ftpConn, $toPath . $file);
- }
- if ($resumeFrom == 0 && ftp_size($this->ftpConn, $toPath . $file) > -1) ftp_delete($this->ftpConn, $toPath . $file);
- if (ftp_put($this->ftpConn, $toPath . $file, $fromPath . $file, FTP_BINARY, $resumeFrom)) {
- echo "successfully uploaded $file\n";
- } else {
- echo "failed uploading $file, retrying\n";
- $resume++;
- if ($resume > 10) {
- echo "failed uploading $file too many times for retry\n";
- } else $this->upload($file, $fromPath, $toPath, $resume);
- }
- }
- function uncompress($path, $file) {
- $fh = gzopen($path . $file, 'rb');
- $outFh = fopen(str_replace('.gz', '', $path . $file), 'wb');
- if (!$fh) die("failed to create file: $file");
- while(!gzeof($fh)) fwrite($outFh, gzread($fh, 8192));
- fclose($outFh);
- gzclose($fh);
- unlink($path . $file);
- echo "successfully uncompressed $file\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement