I’m working with Magento 2, I need to read a file through ftp connection. I can login to ftp but I cannot read the csv file. What I did so far (I cut all the unnecessary parts):
use MagentoFrameworkFileCsv;
use MagentoFrameworkFilesystemIoFtp;
class MyClass {
protected $_csvprocessor;
protected $_ftp;
public function __construct(Csv $csvprocessor, Ftp $ftp) {
$this->_csvprocessor = $csvprocessor;
$this->_ftp = $ftp;
}
public function getCsv() {
$conn = $this->_ftp->open($params); // this works, I can successfully login to ftp
$filecsv = $this->_ftp->read($remotepathtofile); // this gets me the file but it is a string, not an array with the csv data
$this->_csvprocessor->getData($remotepathtofile); // this doesn't work with remote file, only with local path (eg: magentoroot/var/import/file.csv)
}
}
How can I read the csv as an array, as $this->_csvprocessor->getData()
would return, but from remote file instead of local?
3
Answers
You need to parse the csv string. Try using the function “fgetcsv”. See http://php.net/manual/en/function.fgetcsv.php.
Below the code, you can use to read CSV file an array
Hope this will help you enjoy…
You can dump the server file to your local.
where $destinationFilePath is local file path.
Then use that local file.