3

I have a script that pulls a file from an external server, and tries to save it locally (to then be processed). I'm using Varien_Io_Ftp() to facilitate this, however, upon saving the file locally, it throws the following exception, despite creating the file that is required which upsets me.

Warning: chdir(): Invalid argument (errno 22)  in C:\wamp\www\xxxxx\shop\lib\Varien\Io\File.php on line 547

This line in Varien_Io_File is the only one in the vacinity that doesn't have error suppression added to it. Is this a bug, or should I be setting _iwd somewhere within my call?

Hopefully some code to explain the call below:

$importDir = Mage::getBaseDir() . DS . 'var' . DS . 'import' . DS . 'stock' . DS;
$localFile = $importDir . "StockUpdate_" . date("Y-m-d-H-i-s") . ".csv";

$file = new Varien_Io_File();
$file->mkdir($importDir);
$pickupFile = new Varien_Io_Ftp();

try {
    $pickupFile->open(
        array(
            'host'      => $host,
            'user'      => $username,
            'password'  => $password,
            'timeout'   => '10'
        )
    );

    $pickupFile->cd($remoteDir);
    $_fileToImportRemoteTmp = $pickupFile->read($remoteFile);
    $pickupFile->close();

    if (!$file->write($localFile, $_fileToImportRemoteTmp)) {
        die("cannot write local file :/");
    }
    $file->close();

} catch (Exception $e) {
    var_dump($e);
}

The exception I being thrown with this line:

if (!$file->write($localFile, $_fileToImportRemoteTmp)) {
9
  • I dont think you manually need to save the file using $file see second option of string "read" method (string $filename, [string|resource|null $dest = null]) .. "string|resource|null $dest: destination file name, stream, or if null will return file contents". See docs.magentocommerce.com/Varien/Varien_Io/… Commented Nov 5, 2014 at 15:30
  • Added to the question :) Commented Nov 5, 2014 at 16:10
  • Shouldn't '$localFile' be "/path/too/file.csv" instead of "file.csv" Commented Nov 5, 2014 at 16:13
  • I use $file->cd() prior to the "write" line. If I combine the dir & filename, and drop the call to cd(), unfortunatley I get the same error anyway. Commented Nov 5, 2014 at 16:20
  • What is the output of echo $localFile and echo $_fileToImportRemoteTmp Commented Nov 5, 2014 at 16:22

3 Answers 3

3

Try

$importDir = Mage::getBaseDir() . DS . 'var' . DS . 'import' . DS . 'stock' . DS;
$localFile = $importDir . "StockUpdate_" . date("Y-m-d-H-i-s") . ".csv";

$file = new Varien_Io_File();
$file->open(array('path' => $importDir)); 
$file->mkdir($importDir);

$pickupFile = new Varien_Io_Ftp();

try {
    $pickupFile->open(
        array(
            'host'      => $host,
            'user'      => $username,
            'password'  => $password,
            'timeout'   => '10'
        )
    );

    $pickupFile->cd($remoteDir);
    $_fileToImportRemoteTmp = $pickupFile->read($remoteFile);
    $pickupFile->close();

    if (!$file->write($localFile, $_fileToImportRemoteTmp)) {
        die("cannot write local file :/");
    }
    $file->close();

} catch (Exception $e) {
    var_dump($e);
}
3
  • Hi RS - can you expand your answer to clarify why this works (and it does - I came to the same conclusion), and I'll accept it as the answer. Otherwise, I'll put together an answer. Commented Nov 6, 2014 at 13:21
  • Cheers RS. I'll happily add an answer. Just wanted something expanded for anyone coming across this in the future :) - no point accepting this and not pointing out WHY it works in my mind. Commented Nov 6, 2014 at 15:54
  • Good luck next time, if you feel like it need explanation you could alway add a comment, but 2hrs wasted for no reason Commented Nov 6, 2014 at 16:00
2

For anyone looking at this in the future, RS has provided an example that works, but I wanted to jump in and give a little explanation about WHY his solution works.

The exception being thrown at lib\Varien\Io\File.php on line 547 is due to the reference to $this->_iwd not being supressed (and inherently, the fact that it isn't set). I'm assuming this isn't a bug, despite the fact tht you can clearly call the function directly. I'm guessing that the intention is that you call the ->open() function prior to making any action upon a file.

The open() function sets a reference to _iwd (if you pass a path into it to begin with). i.e.

$file->open(array('path' => $importDir));

Grabbing a reference to Varien_Io_File, and making a direct call to cd, whilst possible, doesn't follow the correct flow, therefore when you write, the original reference to _iwd isn't in place.

It's worth pointing out that you need to call:

$file->mkdir($importDir);

prior to calling open().

Additionally, RS noted that http://inchoo.net/magento/magento-code-library/ has an example, however, the inital code doesn't mention the limitation above and actually calls ->write() directly too.

0

Try

$pickupFile = new Varien_Io_Ftp();

$localDir = "/path/to/local/dir";
$remoteFile = "/path/to/remote/dir/text.csv"

mkdir($localDir, 0700, true);


$localFile = $localDir . '/local.csv'
try {
    $pickupFile->open(
        array(
            'host'      => $host,
            'user'      => $username,
            'password'  => $password,
            'timeout'   => '10'
        )
    );

    $pickupFile->read($remoteFile, $localFile);


} catch (Exception $e) {
    var_dump($e);
}

See Varien_Io_Ftp

Also for security you way want to use Varien_Io_Sftp

2
  • It seems like in the limited scope of my example, that this would work (with Varien_Io_Ftp) - however, if I need to manipulate the content before it's saved, I wouldn't be able to. My question remains - is this a bug in Varien_Io_File? Commented Nov 5, 2014 at 16:12
  • It's may be wise to manipulate file locally Commented Nov 5, 2014 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.