Sample code for 30+ languages & platforms
PHP

SFTP Upload and Download to a BinData Object

See more SFTP Examples

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

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\BinData;
use Chilkat2\SFtp;

// 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();

    $bdA = new BinData();
    $bdA->LoadFile('qa_data/jpg/penguins.jpg');

    // Upload the contents of bdA to the SSH/SFTP server.
    $remotePath = 'sftpTesting/penguins.jpg';
    $sftp->UploadBd($bdA, $remotePath);

    // Download the file..
    $bdB = new BinData();
    $sftp->DownloadBd($remotePath, $bdB);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// Verify that bdA and bdB have the exact same contents.
echo 'size of bdA: ', $bdA->NumBytes, "\n";
if ($bdA->ContentsEqual($bdB)) {
    echo "Contents are equal. Success.\n";
} else {
    echo "Contents are NOT equal.  Failed.\n";
}

$sftp->Disconnect();