Sample code for 30+ languages & platforms
Zig

PassiveUseHostAddr

See more FTP Examples

Some FTP servers need this option for passive data transfers. In passive mode, the data connection is initiated by the client sending a PASV command to the FTP server, and the FTP server responds with the IP address and port number where it is listening for the client's connection request. When the PassiveUseHostAddr property is set, the IP address in the PASV response is discarded and the IP address of the remote endpoint of the existing control connection is used instead.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const ftp = try chilkat.Ftp2.init();
    defer ftp.deinit();

    ftp.setHostname("ftp.example.com");
    ftp.setUsername("login");
    ftp.setPassword("password");

    // Connect and login to the FTP server.
    ftp.connect() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Set the PassiveUseHostAddr property.
    ftp.setPassiveUseHostAddr(true);

    // Change to the remote directory where the file will be uploaded.
    ftp.changeRemoteDir("junk") catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Upload a file.
    const local_filename = "hamlet.xml";
    const remote_filename = "hamlet.xml";

    ftp.putFile(local_filename, remote_filename) catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    try ftp.disconnect();

    std.debug.print("File Uploaded!\n", .{});
}