1

I'm having a good deal of trouble sending a file from a linux server to a windows server over SFTP via PHP.
I seem to be connecting just fine, but it always throws an error that I can't create the file on the remote server's end. It's possible that I am messing up at the syntax for the file location.

I have tried two ways now, one using ssh2_scp_send(), and another trying fopen(ssh2.sftp://D:/path/file.csv)

Also, logging into the sftp server via a client puts me at my home folder (ie D:\path\to\home) but if I do a ssh2_exec($connection, 'cd'); and print the stream to the screen, it shows me that my ssh session is currently in the windows filesystem on the C drive.

I was hoping someone would have some advice on this. And I'm not married to this method. I'm using php on my end because it's all coming from a drupal module, but I could always try and incorporate another method.

Jay
  • 93
  • 1
  • 5
  • 1
    http://stackoverflow.com/questions/717854/sftp-from-within-php – ajreal Dec 08 '10 at 17:29
  • Have you looked at CURL? It has the ability to send via SFTP. – Darren Dec 08 '10 at 18:29
  • I haven't yet, but that's my next plan. I'm just hoping that I'm getting the path correct for the remote windows server. Im not familiar with formatting it for windows, and if I need to include the drive in the path, etc. – Jay Dec 08 '10 at 18:49

2 Answers2

1

If all you want to do is send one file, curl works, I suppose, but if you want to do anything more - like maybe verifying that the file has been uploaded, upload multiple files, use publickey authentication, or whatever, curl just isn't versatile enough for that.

My recommendation would be to use phpseclib, a pure PHP SFTP implementation.

0

CURL can use th sftp library.

$localfile = 'test.txt';
  $ch = curl_init();
  $fp = fopen ($localfile, "r") or die('Cannot open textfile');
  curl_setopt($ch, CURLOPT_URL,"sftp://123.123.123.123:10022/username/test.txt");
  curl_setopt($ch, CURLOPT_USERPWD, "username:password");
  curl_setopt($ch, CURLOPT_UPLOAD, 1);
  curl_setopt($ch, CURLOPT_INFILE, $fp);
  if(curl_exec($ch)){
    echo 'File was successfully transferred using SFTP';
  }else{
    echo 'File was unable to be transferred using SFTP';
  }

  curl_close ($ch);
rook
  • 66,304
  • 38
  • 162
  • 239