Sample code for 30+ languages & platforms
Perl Requires Chilkat v9.5.0.71+

SFTP Fsync -- Flush an Open File on the Server

See more SFTP Examples

Demonstrates how to flush the contents of an open file on the server. This example only works for servers that implement the fsync@openssh.com extension.

Chilkat Perl Downloads

Chilkat2 Perl
use strict;
use warnings;
use Chilkat2;

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

my $sftp = Chilkat2::SFtp->new;

# Pass a domain or IP address..
my $success = $sftp->Connect('my-sftp-server.com', 22);
if ($success) {
    $success = $sftp->AuthenticatePw('mySFtpLogin', 'mySFtpPassword');
}

if ($success) {
    $success = $sftp->InitializeSftp();
}

if (!$success) {
    print $sftp->LastErrorText, "\n";
    exit;
}

# Open a file on the server for writing.
my $handle = $sftp->OpenFile('myTest.txt', 'writeOnly', 'createTruncate');
if (!$sftp->LastMethodSuccess) {
    print $sftp->LastErrorText, "\n";
    exit;
}

# Write some text to the file:
$success = $sftp->WriteFileText($handle, 'ansi', 'abcdefghijklmnopqrstuvwxyz');
if (!$success) {
    print $sftp->LastErrorText, "\n";
    exit;
}

# Make sure the server flushes what we wrote to the disk..
# (this is requires a server that implements the fsync@openssh.com SFTP protocol extension)
$success = $sftp->Fsync($handle);
if (!$success) {
    print $sftp->LastErrorText, "\n";
    exit;
}

# ....
# ....

$success = $sftp->WriteFileText($handle, 'ansi', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
if (!$success) {
    print $sftp->LastErrorText, "\n";
    exit;
}

# Close the file.
$success = $sftp->CloseHandle($handle);
if (!$success) {
    print $sftp->LastErrorText, "\n";
    exit;
}

print "Success.\n";