PHP
Chilkat2 PHP
FTP Download with Progress Event Callbacks
See more FTP Examples
FTP download with progress event callbacks.Chilkat PHP Downloads
<?php
use Chilkat2\Ftp2;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
$ftp = new Ftp2();
$ftp->onPercentDone(function (int $percentDone): bool {
echo 'Percent Done: ', $percentDone, "\n";
return false; // return true to abort the method in progress
});
$ftp->onProgressInfo(function (string $name, string $value): void {
echo $name, ': ', $value, "\n";
});
$ftp->Hostname = 'ftp.someFtpServer.com';
$ftp->Username = 'myLogin';
$ftp->Password = 'myPassword';
// Connect and login to the FTP server.
try {
$ftp->ConnectOnly();
$ftp->LoginAfterConnectOnly();
// Change to the remote directory where the file is located.
// This step is only necessary if the file is not in the home directory
// of the FTP account.
$ftp->ChangeRemoteDir('junk');
$localFilename = 'c:/temp/hamlet.xml';
$remoteFilename = 'hamlet.xml';
// Ensure that we get PercentDone callbacks.
$ftp->AutoGetSizeForProgress = true;
// Download the file.
$ftp->GetFile($remoteFilename, $localFilename);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$ftp->Disconnect();
echo "File Downloaded!\n";