1. Go to this page and download the library: Download idct/sftp-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
idct / sftp-client example snippets
use IDCT\Networking\Ssh\Auth\Credentials;
use IDCT\Networking\Ssh\SftpClient;
$client = new SftpClient();
$client->setCredentials(Credentials::withPassword('alice', 'super-secret'));
$client->connect(
host: 'sftp.example.com',
port: 22,
timeoutSeconds: 5,
expectedFingerprint: 'a1b2c3...your sha1 hex...',
);
$client->upload('/local/path/file.bin', '/remote/incoming/file.bin');
$client->download('/remote/outgoing/report.csv', '/local/reports/report.csv');
$client->close();
use IDCT\Networking\Ssh\Auth\Credentials;
use IDCT\Networking\Ssh\Auth\StaticCredentialsLoader;
// password
Credentials::withPassword('alice', 'secret');
// public key
Credentials::withPublicKey('alice', '/path/id_rsa.pub', '/path/id_rsa', 'passphrase-or-null');
// multi-factor: both pubkey AND password legs must succeed
Credentials::withBoth('alice', 'secret', '/path/id_rsa.pub', '/path/id_rsa');
// anonymous (ssh2_auth_none)
Credentials::withNone('guest');
$client->setCredentialsLoader($myVaultLoader); // fires on every connect()
// Mutually exclusive with setCredentials().
use IDCT\Networking\Ssh\Directory\ConflictPolicy;
use IDCT\Networking\Ssh\Directory\SymlinkPolicy;
// Upload a local tree, mirroring its layout under /backup/.
$result = $client->uploadDirectory(
'/local/src',
'/backup/src',
onConflict: ConflictPolicy::Skip, // or Overwrite (default) / Fail
symlinks: SymlinkPolicy::Skip, // or Follow (with cycle detection)
bestEffort: true, // collect failures instead of aborting
);
echo "Uploaded {$result->filesTransferred} files, "
. "{$result->bytesTransferred} bytes, "
. count($result->skipped) . " skipped, "
. count($result->failures) . " failures.\n";
// Mirror it back.
$client->downloadDirectory('/backup/src', '/local/restore');
// Walk arbitrary trees (post-order generator: children before parents).
foreach ($client->walk('/backup') as $entry) {
echo $entry->path . " ({$entry->type->name})\n";
}
// rm -rf on the remote.
$client->removeDirectoryTree('/backup/obsolete');
use IDCT\Networking\Ssh\Retry\ExponentialBackoffRetryPolicy;
use IDCT\Networking\Ssh\Retry\NoRetryPolicy;
$client = new SftpClient(
enableFileSizeVerification: false,
ssh2: null,
retryPolicy: new ExponentialBackoffRetryPolicy(maxRetries: 10, baseMs: 100),
);
// Or swap at runtime.
$client->setRetryPolicy(new NoRetryPolicy());
use IDCT\Networking\Ssh\Exception\SshException;
use IDCT\Networking\Ssh\Retry\RetryPolicyInterface;
final class MyPolicy implements RetryPolicyInterface
{
public function nextDelayMs(int $attempt, SshException $lastError): int
{
return $attempt > 3 ? 0 : 500; // 500 ms each, 3 attempts max
}
}
use IDCT\Networking\Ssh\Checksum\ShellSumRemoteHasher;
use IDCT\Networking\Ssh\Checksum\RedownloadRemoteHasher;
// Option 1: run `sha256sum` on the server (needs shell access).
$client->setRemoteHasher(new ShellSumRemoteHasher('sha256', 'sha256sum'));
// Option 2: re-download the file and hash it locally
// (no server dependency, but doubles transfer time).
$client->setRemoteHasher(new RedownloadRemoteHasher('sha256'));
// All subsequent upload / resumeUpload / download / resumeDownload
// transfers now compare local + remote digests after the transfer and
// throw TransferException on mismatch.
$client->upload('/local/critical.bin', '/remote/critical.bin');