Sample code for 30+ languages & platforms
PHP

SFTP Upload and Download to a StringBuilder Object

See more SFTP Examples

Demonstrates how to SFTP upload from a Chilkat StringBuilder object, and download into a StringBuilder object.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\SFtp;
use Chilkat2\StringBuilder;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$sftp = new SFtp();

// Set some timeouts, in milliseconds:
$sftp->ConnectTimeoutMs = 5000;
$sftp->IdleTimeoutMs = 10000;

// Connect to the SSH server and then authenticate.
$port = 22;
try {
    $sftp->Connect('MY-SSH-SERVER-DOMAIN-OR-IP', $port);
    $sftp->AuthenticatePw('MY-SSH-LOGIN', 'MY-SSH-PASSWORD');
    $sftp->InitializeSftp();

    $sbA = new StringBuilder();
    $sbA->LoadFile('qa_data/hamlet.xml', 'utf-8');

    // Upload the contents of sbA to the SSH/SFTP server.
    $bIncludeBOM = false;
    $remotePath = 'sftpTesting/hamlet.xml';
    $sftp->UploadSb($sbA, $remotePath, 'utf-8', $bIncludeBOM);

    // Download the file..
    $sbB = new StringBuilder();
    $sftp->DownloadSb($remotePath, 'utf-8', $sbB);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// Verify that sbA and sbB have the exact same contents.
echo 'size of sbA: ', $sbA->Length, "\n";
$bCaseSensitive = true;
if ($sbA->ContentsEqualSb($sbB, $bCaseSensitive)) {
    echo "Contents are equal. Success.\n";
} else {
    echo "Contents are NOT equal.  Failed.\n";
}

$sftp->Disconnect();