2017-09-12 7 views
0

Magento 2で作業していますが、ftp接続でファイルを読み込む必要があります。私はftpにログインすることができますが、私はcsvファイルを読むことができません。magento 2 read csv from ftpは文字列ではなく配列を返します

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) 
    } 
} 

$this->_csvprocessor->getData()を返しますが、リモートファイルの代わりに、ローカルからと同じように、私は、配列としてCSVを読むことができますどのように私はこれまでのところ(私はすべての不要な部分をカット)何をしましたか?

答えて

0

、あなたはCSVが

use Magento\Framework\App\Bootstrap; 
    include('app/bootstrap.php'); 
    $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'); 
    $state->setAreaCode('frontend'); 
    $myarray = glob("Book1.csv"); 
    usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);')); 
    if(count($myarray)){ 
     /*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 
     /*End*/ 

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

your code.... 

} 

が、これはあなたが楽しむのに役立ちます願って配列をファイル読み取りに使用することができます...

関連する問題