2

I'm trying to upload a PDF document from a stage server to a remote location using $sftp-put();

CODE:

$sftp = new SFTP($config::SFTP_SERVER);

// login to remote server
if (!$sftp->login($config::SFTP_USER, $config::SFTP_PASSWORD)) {
    throw new Exception('Login failed');
}

// move to relevant directory
$sftp->chdir('fatca');

// upload file
$uploadFile = $sftp->put('test-pdf-upload.pdf', '/srv/www/vhosts/stage.johno.com/fatca/src/uploads/pdfs/345-553453-434__05122017_16:45:26.pdf', NET_SFTP_LOCAL_FILE);

// Error checking for local env only
var_dump($uploadFile);
var_dump($sftp->getSFTPLog());

I'm expecting to view the same PDF, that contains user data and some user uploaded images. I've also confirmed that the original PDF has been created successfully on the staging server, it is intact and shows the relevant information.

The resulting file is created in the new remote server location however it is damaged/unreadable.

The output from var_dump($sftp->getSFTPLog()); is not encouraging either:

bool(false)

What am I doing wrong here? Feel like I've followed the phpseclib documentation well... Although its been one of those long, long days in front of the screen!

Any advice greatly appreciated as always.

  • I'd say post the SSH logs. Not the SFTP logs but the SSH logs. You can get them by doing `define('NET_SSH2_LOGGING', 2);` and then `echo $ssh->getLog();`. And post the results on pastebin.com or dropbox or something. The SSH logs are going to be more useful than the SFTP logs because some errors may happen at the SSH layer and not the SFTP layer. The SFTP layer can be extracted from the SSH layer but not the other way around. – neubert Dec 05 '17 at 17:32
  • Thanks for the reply. Realised what I was doing wrong though, needed to wrap the PDF file location with file_get_contents() to the second parameter of the put() command. Works fine now. – John O'Sullivan Dec 05 '17 at 18:05
  • 1
    @JohnO'Sullivan That's not true, if you use `NET_SFTP_LOCAL_FILE` mode. What version of phpseclib are you using? – Martin Prikryl Dec 05 '17 at 20:11
  • 1
    @MartinPrikryl - good catch - that's actually the issue. He's using 2.0 because he's doing `new SFTP()` instead of `new Net_SFTP()`. For 2.0 you need to do `SFTP::SOURCE_LOCAL_FILE`. – neubert Dec 05 '17 at 21:17

1 Answers1

3

You're using phpseclib 2.0. I can tell because you're doing new SFTP() instead of new Net_SFTP(). For 2.0 you need to do SFTP::SOURCE_LOCAL_FILE. eg.

$uploadFile =
    $sftp->put(
      'test-pdf-upload.pdf',
      '/srv/www/vhosts/stage.johno.com/fatca/src/uploads/pdfs/345-553453-434__05122017_16:45:26.pdf',
      SFTP::SOURCE_LOCAL_FILE);
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
neubert
  • 15,947
  • 24
  • 120
  • 212