Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload file to remote FTP server using Varien_Io_Ftp in Magento?

Tags:

magento

I have csv file and want to upload it to remote FTP server. I see that Magento has Varien_Io_Ftp class, but I cannot find any good documentation or example of its usage. Can somebody help me and give a good example?

like image 878
freento Avatar asked Nov 29 '13 11:11

freento


2 Answers

Here is my solution. Thanks to @SKV.

$ftp = new Varien_Io_Ftp();
$ftp->open(
        array(
                'host'      => $myhost,
                'user'  => $myuser,
                'password'  => $mypass,
            )
        );

$flocal = fopen(Mage::getBaseDir() . DS . 'test.csv', 'r');
$ftp->write('test.csv', $flocal);
$ftp->close();
like image 127
freento Avatar answered Oct 17 '22 00:10

freento


The example below show you how you can create a folder, fetch data via some external SFTP, parse/import, then parse/export it and finally push the data to some external SFTP

Define usable file/folder path etc

$baseDir = Mage::getBaseDir();
$varDir = $baseDir.DS.'var';
$timeOfImport = date('jmY_his');
$importReadyDir = $varDir.DS.'import_ready'.DS.$timeOfImport;
$exportReadyDir = $varDir.DS.'export_ready'.DS.$timeOfImport;
$_fileToImportRemote = '/home/skv/customers.txt';
$_fileToExportRemote = '/home/skv/customers_export.txt';
$_fileToImportBaseName = 'customers.txt';
$_fileToImportLocal = $importReadyDir.DS.'customers.txt';
$_fileToExportLocal = $exportReadyDir.DS.'customers_parsed.txt';

Then we will fetch the file and save it to our magento installation.

$file = new Varien_Io_File();
//Create import_ready folder
$importReadyDirResult = $file->mkdir($importReadyDir);
if (!$importReadyDirResult) {
    //Handle error
}
$sftpPickupFile = new Varien_Io_Sftp();
try {
    $sftpPickupFile->open(
        array(
            'host'      => 'some.server.com',
            'username'  => 'skv',
            'password'  => 'MyPass',
            'timeout'   => '10'
        )
    );
    $_fileToImportRemoteTmp = $sftpPickupFile->read($_fileToImportRemote);
    if(!$_fileToImportRemoteTmp) {
        //Handle error
    }
    $sftpPickupFile->close();
    if (!$file->write($_fileToImportLocal, $_fileToImportRemoteTmp)) {
        //Handle error
    }
} catch (Exception $e) {
    //Handle error
}

now this is the example to send some data to remote server

$flocal = new Varien_Io_File();
$flocal->open(array('path' => $importReadyDir));
$flocal->streamOpen('customers.txt', 'r');
while (false !== ($csvLine = $flocal->streamReadCsv())) {
    //Parse the data and import it...
    //Zend_Debug::dump($csvLine, '$csvLine');
    /**
        $csvLine array(4) {
          [0] => string(13) "skv"
          [1] => string(16) "[email protected]"
          [2] => string(2) "28"
          [3] => string(25) "Sample address for skv"
        }
     */
}
//Now we do reverse, grab some data from Magento and upload it to SFTP
$data = '"skv", "[email protected]", "28", "Sample address for skv"
"skv", "[email protected]", "29", "Sample address for Tomas"';
$sftpDumpFile = new Varien_Io_Sftp();
try {
    $sftpDumpFile->open(
        array(
            'host'      => 'some.server.com',
            'username'  => 'skv',
            'password'  => 'MyPass',
            'timeout'   => '10'
        )
    );
    //Make a local backup that will be send to SFTP
    $file->mkdir($exportReadyDir);
    $file->write($_fileToExportLocal, $data);
    //Upload to SFTP
    $_fileToExportRemoteTmp = $sftpDumpFile->write($_fileToExportRemote, $data);
} catch (Exception $e) {
    echo $e->getMessage();
}
like image 40
Sunil Verma Avatar answered Oct 17 '22 01:10

Sunil Verma