-1

I'm trying to transfer a pdf file from our server to another via SFTP. I've checked the files and they are not inherently corrupt. I can scp them on the command line to my computer and they open just fine. Note: I can successfully transfer the pdf file, it is just corrupted once it gets to the other server.

I've tried using two methods:

conn = ssh2_connect($url, 22);
$auth = ssh2_auth_password($conn, $userName, $password);

$sftp = ssh2_sftp($conn);
file_put_contents("ssh2.sftp://".$sftp.$remoteFilePath, $localFilePath);

After looking at this question, I came upon the below question and tried the given answer:

ssh2_scp_send() using php corrupts pdf

conn = ssh2_connect($url, 22);
$auth = ssh2_auth_password($conn, $userName, $password);

$sftp = ssh2_sftp($conn);
$fp = fopen("ssh2.sftp://".$sftp.$remoteFilePath, "w");
fwrite($fp, $localFilePath);
fclose($fp);

This also transfers the file, but is also corrupted so that I cannot open it in my FTP GUI once it is in their server.

I'm now aware of the other php extension that does this. I'll try it out if I can't get this to work but I've put enough time into this, and it SHOULD work. Any thoughts?

Community
  • 1
  • 1
Pete_1
  • 981
  • 3
  • 14
  • 23
  • make sure the transfer is binary –  Sep 30 '15 at 20:48
  • @Dagon I checked and, for example, file_put_contents PHP says in the docs is binary safe. Is there something I have to do to enable binary that I'm not seeing? – Pete_1 Sep 30 '15 at 20:49
  • fopen should be r not w? and add b so 'rb' *"If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters."* –  Sep 30 '15 at 20:56
  • Really? That produces a whole bunch of errors. My understanding is that I'm opening up a non-existent file for writing (which creates a blank file), and then I write to that newly created file using fwrite(). I tried 'r' and 'rb', which threw a bunch of errors. I also tried 'wb' and it didn't make a difference. – Pete_1 Sep 30 '15 at 21:05
  • sorry i got the direction of transfer wrong ;( but the **b** still applies –  Sep 30 '15 at 21:07

2 Answers2

0

Look at http://php.net/manual/en/function.ssh2-scp-send.php

and try:

$conn = ssh2_connect($url, 22);
$auth = ssh2_auth_password($conn, $userName, $password);

ssh2_scp_send($conn, $localFilePath, $remoteFilePath, 0644);
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
0

Found the answer. I had $localFilePath written as so:

$localFilePath = "/path/to/my/file.pdf";

file_put_contents() and fwrite() were treating the variable as a string. So it would create a new pdf file, with its contents being the string filepath. I only discovered this after trying to transfer a small .txt file. SO, adding this fixed the problem:

$localFilePath = fopen("/path/to/my/file.pdf", "r");

I feel that it's strange that I haven't seen this used in the examples that I've seen that sftp using this approach, but there you go.

Pete_1
  • 981
  • 3
  • 14
  • 23