A SFTP client
Open a command console, enter your project directory and execute:
composer require proglab/sftp-client-bundle
If you're not using symfony/flex, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
// config/bundles.php
return [
// ...
Proglab\SftpClientBundle\SftpClientBundle::class => ['all' => true],
];
You have two choices:
- Create the client manually, and pass it a logger:
use Proglab\SftpClientBundle\Service\SftpClient;
use Psr\Log\NullLogger;
$logger = new NullLogger();
$client = new SftpClient($logger);
- Get the client from Dependency Injection:
use Proglab\SftpClientBundle\Service\SftpClient;
class Service
{
public function __construct(private SftpClient $client)
{
}
}
You must connect to a SFTP server.
You need the username, password, host and port (22 by default).
$client->connect('username', 'password', 'host', 22);
$client->deco();
List files in remote directory. The remote directory path must be absolute.
$files = $client->getRemoteListFiles('/var/www/');
List files in local directory. The local directory path must be absolute.
$files = $client->getLocalListFiles('/var/www/');
Upload a file from local to remote dir:
$files = $client->upload($fileLocalPath, $fileRemotePath, $delete = true);
Download a file from remote to local dir:
$files = $client->download($fileRemotePath, $fileLocalPath, $delete = true);
Synchronize local files to remote directory:
$files = $client->syncLocalDirToRemote($localDir, $remoteDir, $delete = true);
Synchronize remote files to local directory:
$files = $client->syncRemoteDirToLocal($remoteDir, $localDir, $delete = true);
Many thanks to jmsche for his help