magento 2 read csv from ftp returns a string and not an array

Related searches

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 Magento\Framework\File\Csv;
use Magento\Framework\Filesystem\Io\Ftp;

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?

You need to parse the csv string. Try using the function "fgetcsv". See

magento2, magento2 - magento 2 read csv from ftp returns a string and not an array - $this ->_ftp->read($remotepathtofile); // gets me file string, not array� This is not good approach and, to improve that process, we should use directives. Directive is a special shortcode or placeholder that can be replaced by some actual information. This term isn’t new for Magento users, because it came from Magento 1.x and it has the same usage in Magento 2.

Below the code, you can use to read CSV file an array

    use Magento\Framework\App\Bootstrap;
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $objectManager1 = Magento\Framework\App\ObjectManager::getInstance();
    $directoryList = $objectManager1->get('\Magento\Framework\App\Filesystem\DirectoryList');
    $path = $directoryList->getPath('media');
    $state = $objectManager->get('Magento\Framework\App\State');
    $myarray = glob("Book1.csv"); 
    usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
        /*This will create an array of associative arrays with the first row column headers as the keys.*/
        $csv_map = array_map('str_getcsv', file($myarray[count($myarray)-1]));
        array_walk($csv_map, function(&$a) use ($csv_map) {
          $a = array_combine($csv_map[0], $a);
        array_shift($csv_map); # remove column header

        $message = '';
        $count   = 1;
        foreach($csv_map as $data){ 

your code....


Hope this will help you enjoy...

Import csv in magento 2 is not working and show error , I get the same issue and it's related to a magento2 bug where custome import without any sku column it's failed. you can have a patch here :. Magento 2 Developer Documentation. Magento 2.4 Developer Documentation. Everything you need to build and manage a customized Magento store.

You can dump the server file to your local.

   $filecsv = $this->_ftp->read($remotepathtofile, $destinationFilePath);

where $destinationFilePath is local file path. Then use that local file.

How to Read a CSV File & Convert It into an Array in Magento 2 , How to Read a CSV File & Convert It into an Array in Magento 2 And the problem of reading CSV files and converting them into an array is not Different file sources let you import data files from Dropbox, FTP, and SFTP. Patch (Composer package 2.3.4-p2) is a security-only patch that provides fixes for vulnerabilities that have been identified in our previous quarterly release, Magento 2.3.4. All hot fixes that were applied to the 2.3.4 release are included in this security-only patch.

How to read CSV data as array pairs in Magento 2?, Read CSV file as array using Best practice to retrieve the data from Read CSV file using Magento 2 to retrieve all the records from the @return string[]. */ In the Block file, I have passed the CSV file path as $file variable. first, verify CSV exists or not, if file exists, convert it to an array and gives an output. Return Value: An array with the CSV fields on success, NULL if an invalid file is supplied, FALSE on other errors and on EOF: PHP Version: 4.0+ Binary Safe: Yes, in PHP 4.3.5: PHP Changelog: PHP 5.3 - Added the escape parameter

Handling files and folders in Magento, Magento code library – Handling files and folders Most of the time, standard Magento product/customer import/export is not sufficient for the task at hand. '$ csvLine'); /** $csvLine array(4) { [0] => string(13) "Branko Ajzele" [1] => string(16) "" [2] => string(2) "28" [3] => string(25) "Sample� The group can be specified as a path-like string, e.g., to access subgroup ‘bar’ within group ‘foo’ pass ‘/foo/bar’ as the group argument. When writing multiple groups in one file, pass mode='a' to to_netcdf to ensure that each call does not delete the file. Data is always loaded lazily from netCDF files. You can manipulate, slice

Read a comma-separated values (csv) file into DataFrame. Also supports optionally iterating or breaking of the file into chunks. Additional help can be found in the online docs for IO Tools. Parameters filepath_or_buffer str, path object or file-like object. Any valid string path is acceptable. The string could be a URL.