0

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.

Znk
  • 29
  • 7
  • file_get_contents() might be simpler. This answer has examples of that and ftp_connect(): https://stackoverflow.com/a/43505825/3103434 – Spudly Jul 02 '20 at 07:24
  • `file_get_contents` didn't worked for some reason So instead I used fopen('php://temp', 'r+');... as in the SO. Now I have issue with how to read the CSV records. I will update what I did – Znk Jul 02 '20 at 08:04
  • What is the error you're getting? – Spudly Jul 02 '20 at 16:39

0 Answers0