Sample code for 30+ languages & platforms
PHP

FTP Upload with Progress Event Callbacks

See more FTP Examples

FTP upload with progress event callbacks.

Chilkat PHP Downloads

PHP
<?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 = 'my_ftp_username';
$ftp->Password = 'my_ftp_password';

// Connect and login to the FTP server.
try {
    $ftp->Connect();

    // Change to the remote directory where the file will be uploaded.
    $ftp->ChangeRemoteDir('junk');

    // Upload a file.
    $localPath = 'c:/temp/hamlet.xml';
    $remoteFilename = 'hamlet.xml';

    $ftp->PutFile($localPath, $remoteFilename);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$ftp->Disconnect();

echo "File Uploaded!\n";