Skip to content

SFTP put sometimes mistakenly sending file to root directory #2042

Description

@ShaneOH

Hello -- we have an automation that sends a file to an SFTP server in some specific directory e.g. /ftp/some/dir. The user who owns this SFTP server informed us that every once in awhile, seemingly at random, the file will end up in their root directory instead of the correct directory. We're sending ~50-100 files a day.

I put a bit of logging code in:

            $this->debugInfo[] = [
                'operation' => 'sendFile',
                'file_name' => basename($config->localFile),
                'current_dir' => $this->connection->pwd(),
            ];

            if (!$this->connection->put(
                remote_file: basename($config->localFile),
                data: $config->localFile,
                mode: NetSFTP::SOURCE_LOCAL_FILE
            )) {
                $this->throwError('Failed to send file: ' . $config->localFile);
            } elseif (!$this->connection->file_exists(basename($config->localFile))) {
                $this->throwError('Failed to confirm sent file: ' . basename($config->localFile));
            }

And $this->connection is initialized like so:

        $this->connection = new phpseclib3\Net\SFTP($this->address, $this->config['port'] ?? 21);

        try {
            if (!$this->connection->login($this->config['username'], $this->config['password'])) {
                $this->throwError('Failed to connect to SFTP server: ' . $this->address);
            }
        } catch (UnableToConnectException) {
            $this->throwError('Failed to connect to SFTP server: ' . $this->address);
        }

        if (!empty($this->config['path'])) {
            $this->chdir($this->config['path']);
        }

So we chdir in the beginning, and our logs show that that $this->connection->pwd() command is printing the right output (i.e. ftp/some/dir, and yet, this was printed for one of the files that ended up in the root directory instead.

Any idea how to even begin debugging this? Is this a known issue? We're considering just doing an extra sweep for the file in the root directory after send and then moving it to the correct directory if found just as a safeguard option, but that likely isn't the ideal solution and I'd like to understand how this could be happening in the first place.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions