thephpleague/flysystem-sftp

Trying to access a directory in the filesystem root leads to a malformed path (//directory)

nikoheikkila opened this issue · 1 comments

I have a connection to an SFTP server (using Lumen 5.8) as following:

/* config/filesystems.php */
'remote' => [
    'driver' => 'sftp',
    'host' => ...,
    'port' => ...,
    'username' => ...,
    'password' => ...,
    'root' => '',
    'folder' => 'inbox', // custom option passed to listDirectoryContents() method
    'timeout' => 30,
];

My purpose is to connect to a remote SFTP path called /inbox which is a directory directly in a filesystem root. However, this fails due to the fact that SftpAdapter class has the prefix() method which returns value //inbox due to perhaps incorrect string trimming. I used XDebug to verify this behaviour with version 1.0.21 checking what is returned from the aforementioned method.

If I specify for example /home/sftp as the root option the method returns /home/sftp/inbox properly.

This might cause issues with some older or non-standard SFTP servers where the directory path can't begin with //.

Of course, I could be able to use value /inbox as the root option but I also need to read/write on other folders in filesystem root where this would become inconvenient quickly.

Thanks in advance for handling this. 👍

It seems that the root cause is in the setConnectionRoot() method.

It concatenates path to the working directory with the separator which in this case results in double slash value. Perhaps this method could be refactored to add the separator only when needed? Opened a PR for this suggestion, see below.