Skip to content

Commit

Permalink
Use native FTP connection extension
Browse files Browse the repository at this point in the history
  • Loading branch information
amattu2 committed Jan 6, 2022
1 parent 83318a5 commit 0a7fec5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
49 changes: 26 additions & 23 deletions FTP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
10 changes: 7 additions & 3 deletions test-ftp-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<br><br>";
} else {
Expand All @@ -165,4 +165,8 @@
}

// This will delete the file that we just made
$ftpWrapper->cleanUp();
if ($ftpWrapper->cleanUp()) {
echo "Cleaned up the workspace<br><br>";
} else {
echo "Failed to clean up the workspace<br><br>";
}

0 comments on commit 0a7fec5

Please sign in to comment.