From 0a7fec58a634c1a446ed757d62d587c0dc52021f Mon Sep 17 00:00:00 2001 From: Alec M Date: Thu, 6 Jan 2022 17:05:09 -0500 Subject: [PATCH] Use native FTP connection extension --- FTP.class.php | 49 ++++++++++++++++++++++++--------------------- test-ftp-upload.php | 10 ++++++--- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/FTP.class.php b/FTP.class.php index f1b6f37..441a1ff 100644 --- a/FTP.class.php +++ b/FTP.class.php @@ -35,7 +35,14 @@ class FTP { * * @var string */ - private const HOST = "ftp://data.carfax.com"; + private const HOST = "data.carfax.com"; + + /** + * VHR FTP port + * + * @var int + */ + private const PORT = 21; /** * Report File Header Fields @@ -297,6 +304,16 @@ public function upload() : bool throw new FileUploadedException("The file has already been uploaded to the FTP server"); } + // Check if ftp_connect is available + if (!function_exists("ftp_connect")) { + throw new \Exception("FTP extension is not available on this server"); + } + + // Check if report file exists + if (!file_exists($this->filename)) { + return false; + } + // Check to see if the header has been written if (!$this->has_header) { return false; @@ -317,31 +334,17 @@ public function upload() : bool return false; } - // Connect to FTP server with cURL - $ch = curl_init(); - $this->handle = fopen(__DIR__ . "/" . $this->filename, "r"); - if ($this->handle && flock($this->handle, LOCK_EX)) { - curl_setopt($ch, CURLOPT_URL, "ftp://" . SELF::HOST . "/" . $this->filename); - curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); - curl_setopt($ch, CURLOPT_UPLOAD, 1); - curl_setopt($ch, CURLOPT_INFILE, $this->handle); - curl_setopt($ch, CURLOPT_INFILESIZE, filesize($this->filename)); - - // Execute the request - $exec = curl_exec($ch); - $err = curl_error($ch); - - // Clean up - curl_close($ch); - flock($this->handle, LOCK_UN); - $this->was_uploaded = $exec && !$err; + // Open FTP Connection and upload + if (($ftp = @ftp_connect(FTP::HOST, FTP::PORT)) && @ftp_login($ftp, $this->username, $this->password)) { + // Push file + $status = ftp_put($ftp, $this->filename, $this->filename, FTP_BINARY); + + // Close Connection + $this->was_uploaded = $status; + ftp_close($ftp); } // Return - fclose($this->handle); - $this->handle = null; return $this->was_uploaded; } diff --git a/test-ftp-upload.php b/test-ftp-upload.php index 3703533..239d03a 100644 --- a/test-ftp-upload.php +++ b/test-ftp-upload.php @@ -107,7 +107,7 @@ AND a.TicketType = 'Invoice' AND CHAR_LENGTH(c.VIN) = 17 ORDER BY a.EstNum ASC - LIMIT 300020, 25 + LIMIT 300020, 250 "; // Run the query @@ -156,7 +156,7 @@ // Upload the file // TODO: Uncomment the FTP call to upload the file -//$uploaded = $ftpWrapper->upload(); +$uploaded = $ftpWrapper->upload(); if ($uploaded) { echo "Successfully uploaded file to FTP server

"; } else { @@ -165,4 +165,8 @@ } // This will delete the file that we just made -$ftpWrapper->cleanUp(); \ No newline at end of file +if ($ftpWrapper->cleanUp()) { + echo "Cleaned up the workspace

"; +} else { + echo "Failed to clean up the workspace

"; +} \ No newline at end of file