3

Using the PHP plugin phpseclib v2.0.18 with PHP 7.3, when I try to upload a file to the SFTP server, the put() method returns true, but the file is never uploaded.

The log say that everything is ok.

My code:

foreach ($schedules as $idx => $schedule)
{
    $company = \Company::getItem($schedule->id_login);

    $outgoing_path = concat_path(BASE_PATH, 'pdf', 'file_schedules', $company->cif, 'outgoing', date('Ymd'));

    if (!is_dir($outgoing_path)) create_folder($outgoing_path, 0775, true);

    if ( ! ($sftp = new SFTP($schedule->host, $schedule->port)))
    {
        static::logError('No se pudo iniciar la conexión.', array($schedule->id_login, $company->name, $company->cif));
        continue;
    }

    if (!$sftp->login($schedule->user, $schedule->password))
    {
        static::logError('No se pudo hacer login en el servidor remoto.', array($schedule->id_login, $company->name, $company->cif));
        continue;
    }

    unset($schedule->password);

    if (!$sftp->chdir(concat_path($schedule->path, 'outgoing')))
    {
        static::logError('No se pudo acceder al directorio.', $schedule);
    }

    $dir_iterator = new \DirectoryIterator($outgoing_path);

    $count = 0;

    foreach ($dir_iterator as $file)
    {
        if ($file->isDot()) continue;

        if ($sftp->put($file->getPathname(), concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)))
        {
            $count += 1;
        }
    }

    static::logInfo("$count archivos exportados.");
}

Login is fine. The local and remote paths and filenames are both fine. Similar code for download files works fine. File permissions are fine.

What am I doing wrong?

The log output:

-> NET_SFTP_INIT (0s)
00000000  00:00:00:03                                      ....

<- NET_SFTP_VERSION (0.008s)
00000000  00:00:00:03:00:00:00:18:70:6f:73:69:78:2d:72:65  ........posix-re
00000010  6e:61:6d:65:40:6f:70:65:6e:73:73:68:2e:63:6f:6d  name@openssh.com
00000020  00:00:00:01:31:00:00:00:13:73:74:61:74:76:66:73  ....1....statvfs
00000030  40:6f:70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:01  @openssh.com....
00000040  32:00:00:00:14:66:73:74:61:74:76:66:73:40:6f:70  2....fstatvfs@op
00000050  65:6e:73:73:68:2e:63:6f:6d:00:00:00:01:32:00:00  enssh.com....2..
00000060  00:14:68:61:72:64:6c:69:6e:6b:40:6f:70:65:6e:73  ..hardlink@opens
00000070  73:68:2e:63:6f:6d:00:00:00:01:31:00:00:00:11:66  sh.com....1....f
00000080  73:79:6e:63:40:6f:70:65:6e:73:73:68:2e:63:6f:6d  sync@openssh.com
00000090  00:00:00:01:31                                   ....1

-> NET_SFTP_REALPATH (0s)
00000000  00:00:00:01:2e                                   .....

<- NET_SFTP_NAME (0.003s)
00000000  00:00:00:01:00:00:00:0c:2f:55:73:65:72:73:2f:6a  ......../Users/j
00000010  6f:72:67:65:00:00:00:0c:2f:55:73:65:72:73:2f:6a  orge..../Users/j
00000020  6f:72:67:65:00:00:00:00                          orge....

-> NET_SFTP_OPENDIR (0s)
00000000  00:00:00:15:2f:55:73:65:72:73:2f:6a:6f:72:67:65  ..../Users/jorge
00000010  2f:6f:75:74:67:6f:69:6e:67                       /outgoing

<- NET_SFTP_HANDLE (0.0032s)
00000000  00:00:00:04:00:00:00:00                          ........

-> NET_SFTP_CLOSE (0s)
00000000  00:00:00:04:00:00:00:00                          ........

<- NET_SFTP_STATUS (0.0031s)
00000000  00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00  ........Success.
00000010  00:00:00                                         ...

-> NET_SFTP_OPEN (0.0001s)
00000000  00:00:00:6e:2f:55:73:65:72:73:2f:6a:6f:72:67:65  ...n/Users/jorge
00000010  2f:4d:45:47:41:73:79:6e:63:2f:77:6f:72:6b:73:70  /MEGAsync/worksp
00000020  61:63:65:2f:74:65:73:6f:72:69:7a:61:2f:6d:69:6c  ace/tesoriza/mil
00000030  65:73:74:6f:6e:65:73:2f:70:64:66:2f:66:69:6c:65  estones/pdf/file
00000040  5f:73:63:68:65:64:75:6c:65:73:2f:41:38:36:32:31  _schedules/A8621
00000050  32:34:32:30:2f:6f:75:74:67:6f:69:6e:67:2f:32:30  2420/outgoing/20
00000060  31:39:31:30:30:37:2f:50:72:75:65:62:61:2e:78:6c  191007/Prueba.xl
00000070  73:78:00:00:00:1a:00:00:00:00                    sx........

<- NET_SFTP_HANDLE (0.0047s)
00000000  00:00:00:04:00:00:00:00                          ........

-> NET_SFTP_WRITE (0s)
00000000  00:00:00:04:00:00:00:00:00:00:00:00:00:00:00:00  ................
00000010  00:00:00:23:2f:55:73:65:72:73:2f:6a:6f:72:67:65  ...#/Users/jorge
00000020  2f:6f:75:74:67:6f:69:6e:67:2f:50:72:75:65:62:61  /outgoing/Prueba
00000030  2e:78:6c:73:78:2f:31                             .xlsx/1

<- NET_SFTP_STATUS (0.0032s)
00000000  00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00  ........Success.
00000010  00:00:00                                         ...

-> NET_SFTP_CLOSE (0s)
00000000  00:00:00:04:00:00:00:00                          ........

<- NET_SFTP_STATUS (0.0031s)
00000000  00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00  ........Success.
00000010  00:00:00                                         ...

Seems to be alright.

Jomiquel
  • 45
  • 4
  • Assuming the file is a small one can you post the SFTP logs? You can get them by doing `define('NET_SFTP_LOGGING', 2);` at the top and then `echo $sftp->getSFTPLog();` after you get the error. – neubert Oct 08 '19 at 10:58

1 Answers1

3

From your code:

        if ($sftp->put($file->getPathname(), concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)))
        {
            $count += 1;
        }

Let's break that down some. So you're doing this:

$sftp->put(
    $file->getPathname(),
    concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)
);

idk what concat_path does but my guess is that you have the placement of the parenthesis wrong. My guess is that you want to be doing this:

$sftp->put(
    $file->getPathname(),
    concat_path($sftp->pwd(), $file->getFilename()),
    SFTP::SOURCE_LOCAL_FILE
);

Anyway, where are you looking for the files? Per your logs they should be showing up at /Users/jorge/MEGAsync/workspace/tesoriza/milestones/pdf/file_schedules/A86212420/outgoing/20191007/Prueba.xlsx. Is that where you're looking?

neubert
  • 15,947
  • 24
  • 120
  • 212
  • 1
    You're right @neubert, the placement of the parenthesis was wrong. Can't understand I couldn't realize! Thanks. And, yes, you are right again. I was interchanged the local file and the remote file parameters in the calling. This last mistake has been easier to find once you helped me to resolve the first one. Thank you very much. – Jomiquel Oct 10 '19 at 09:48
  • I'm sorry @neubert. My reputation at stack overflow is less than 15, so my vote for your answer will stay hidden by now. – Jomiquel Oct 10 '19 at 17:52