2

I am using the PECL ssh2 module to output XML data to a sftp server. I have two entirely separate PHP scripts which gather different data and send the output to different file on the stfp server.

CUSTOMER EXPORT:

$conn = ssh2_connect(SFTP_SERVER, SFTP_PORT);
ssh2_auth_password($conn, SFTP_USER, SFTP_PWD);
$sftp = ssh2_sftp($conn);
$file = 'ssh2.sftp://' . $sftp . CUSTOMER_EXPORT_PATH . CUSTOMER_EXPORT_FILENAME;
$doc = new DOMDocument('1.0','UTF-8');
CustomerExportXML($doc);
if (file_exists($file)) {
    unlink($file);
}
$bytes_saved = $doc->save($file);

PRODUCT EXPORT:

$conn = ssh2_connect(SFTP_SERVER, SFTP_PORT);
ssh2_auth_password($conn, SFTP_USER, SFTP_PWD);
$sftp = ssh2_sftp($conn);
$file = 'ssh2.sftp://' . $sftp . PRODUCT_EXPORT_PATH . PRODUCT_EXPORT_FILENAME;
$doc = new DOMDocument('1.0','UTF-8');
ProductExportXML($doc);
if (file_exists($file)) {
    unlink($file);
}
$bytes_saved = $doc->save($file);

In each case the XxxExportXML($doc) function takes a couple of minutes to gather the relevant data and stuff it in to $doc.

Each script works as is and exports the correct data to the correct place.

The problem is when their execution overlaps only the last one executed actually writes to the sftp server. If I echo out the $file variable then in each case they both have the same resource ID ie ssh2.sftp://ResourceID#150/Customer/Customer.xml and ssh2.sftp://ResourceID#150/Product/Product.xml

So my question is why are these two processes interfering with each other and how do I fix it so they can both be run at the same time?

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Gordy
  • 91
  • 1
  • 8

1 Answers1

0

So they're two different scripts? That's... weird. Maybe try phpseclib, a pure PHP SFTP implementation, instead. eg.

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');
neubert
  • 15,947
  • 24
  • 120
  • 212
  • Thanx for this suggestion. I did have a look at phpseclib as an alternative. I decided not to go with it because it is TOO pure. That is to say that it can transfer files in a true sftp manner etc, but it cannot be used as a file handle in PHP as you can with the ssh2 mod. This is important to me as the files produced can be large and lots of them can be requested at once meaning that if I had to write them out locally first and then transfer them I would suffer with disk space and band width. – Gordy Feb 01 '13 at 10:44
  • I'm a bit skeptical that it can't be done with phpseclib. What does ``$doc->save($file)`` do? Seems like whatever it does phpseclib could do. Maybe you're writing bytes to the remote filesystem as they're made available? If that's the case then it seems like you could just use ``NET_SFTP_RESUME`` as the third parameter for ``$sftp->put()``. That'll append data to the file instead of overwriting the file. – neubert Feb 01 '13 at 16:18
  • `$doc->save($file)` is the `DOMDocument::save($fh)` method which you can see in the [PHP Manual](http://www.php.net/manual/en/domdocument.save.php). So in this case `$file` holds a url file handle which can then just be passed around as required. phpseclib does not have a file handle style property. Thus to use it I would have to either get the XML string written to memory or to local disk first and then transfer it using phpseclib. I do not have enough disk space or memory to do this. I cannot do it on the fly as the data is not in string format, but tied up as a DOMDocument object. – Gordy Feb 12 '13 at 15:50
  • Would a stream wrapper for sftp / phpseclib help? http://www.frostjedi.com/phpbb3/viewtopic.php?p=390430#p390430 is such a wrapper although from the comments in that topic it sounds like the phpseclib author is working on his own version.. – neubert Feb 12 '13 at 16:23