I have a filename test.csv
at my FTP Server. I am working on reading this file and import to the table.
So far I created a class as follows:
class FtpHelpers {
protected $ftpOpen;
public function __construct()
{
$this->ftpHost = 'www.abc.com';
$this->ftpUsername = 'username';
$this->ftpPassword = 'password';
if (is_null($this->ftpHost)) {
throw new Exception("FTP references are missing or incomplete.");
}
if (is_null($this->ftpUsername) || is_null($this->ftpPassword)) {
throw new Exception("FTP credentials are missing or incomplete.");
}
$this->ftpConnection = ftp_connect($this->ftpHost);
if($this->ftpConnection === false) {
throw new Exception("Could not connect to the FTP server.");
}
$loginResults = ftp_login($this->ftpConnection, $this->ftpUsername, $this->ftpPassword);
if ($loginResults === false ) {
throw new \Exception("The credentials were refused by the FTP server.");
}
ftp_pasv($this->ftpConnection, true);
}
public function getDataList($targetFolder)
{
$this->ftpOpen = fopen('php://temp', 'r+');
ftp_fget($this->ftpConnection, $this->ftpOpen, $targetFolder, FTP_BINARY, 0);
$ftpStat = fstat($this->ftpOpen);
fseek($this->ftpOpen, 0);
return fread($this->ftpOpen, $ftpStat['size']);
}
}
This class I inject to other class where I need to import. When I try to print $this->ftpHelpers
in other controllers, I get connection result.
My question is what next I should do to read this test
file.