8

I transfer files with PHP and SSH2 to an remote server.

I use this:

$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);

But sometimes the file on the remote server is incomplete. I have the assumption that SSH2 doesn't transfer the EOF or anything else.

Do you have any ideas or solutions?

Simon Stamm
  • 113
  • 1
  • 6

1 Answers1

22

The problem is that you don't close the SSH session. So the internal buffers aren't flushed and the files aren't written to disk fully.

Here is a workaround - just close the session with:

ssh2_exec($connection, 'exit');

This will cause all buffers to be flushed and your files should be transfered completely.

Hope, that helps.

msiemens
  • 2,223
  • 1
  • 28
  • 38
  • Welcome to Stack Overflow! Completely off-topic side note: Your home page has some odd *Umbrüche* in Chrome 14 e.g. `Pr-ogrammiersprachen` and `Nebenb-eruflich`. Not sure why, but I thought you might want to know. @Simon if this answer worked for you, consider marking it accepted using the check mark symbol. – Pekka Oct 16 '11 at 15:07
  • 1
    Thanks Pekka, but I must wait 5 minutes, before I can accept it. :-) – Simon Stamm Oct 16 '11 at 15:10
  • @Pekka: I used [code.google.com/p/hyphenator](http://code.google.com/p/hyphenator) to automatically add hyphenations. But it really seems a strange hyphenation. Just can't confirm it in Chrome 14. (Checked it and the hyphenations are correct...) – msiemens Oct 16 '11 at 15:13
  • Umm... guys, you both seem to be from the same place. While it's perfectly okay to answer each others' questions if you're friends, it's not *exactly* how it's supposed to work and do be careful when voting for each other. They have scripts to detect that kind of thing. – Pekka Oct 16 '11 at 15:13
  • @m--s Sorry, I'm on my laptop where I use Chrome 13, not 14. Definitely looks weird here - maybe because my primary `accept-language` is English? – Pekka Oct 16 '11 at 15:14
  • @Pekka: May really be, because hyphenator checks the browser language I think. – msiemens Oct 16 '11 at 15:19
  • @Pekka: Concerning your comment, that Simon Stamm and I are from the same place: semms that is correct, we both have set _Germany_ as our location just as you did... – msiemens Oct 16 '11 at 15:21
  • @m--s No, I mean that you happen to live [10 km apart from each other](http://maps.google.de/maps?q=Beekstr.+17+Sehnde+to:+Ferdinand-Wahrendorff-Str.+14+Sehnde-Ilten&saddr=Beekstr.+17+Sehnde&daddr=Ferdinand-Wahrendorff-Str.+14+Sehnde-Ilten&hl=en&sll=51.151786,10.415039&sspn=14.985744,43.286133&geocode=FZT1HQMdGkSXAClJTNMNJQiwRzEeeqomleoh8Q%3BFWi8HgMdjJWXACn9WbsP6QiwRzEWiFMyVqKr5A&vpsrc=0&t=h&z=13). And [do work together.](http://musikschule-kalinka.de/) Come *on.* – Pekka Oct 16 '11 at 15:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4308/discussion-between-m-s-and-pekka) – msiemens Oct 16 '11 at 15:27
  • @Pekka: I like you. :-) You remind me of me in researching. ;-) – Simon Stamm Oct 16 '11 at 16:29
  • @Simon well, you will find many fast Googlers on these sites. ;) – Pekka Oct 16 '11 at 16:30
  • Your solution worked for me! Apparently calling `ssh2_disconnect` was not enough. It would be nice if this were in the official documentation, although it is discussed in the comments: https://www.php.net/manual/en/function.ssh2-scp-send.php Incidentally the report there is from it happening on Windows; I experienced it on Linux (both boxes) so it is evidently a cross-platform problem. – cazort Jan 19 '22 at 21:26