networking intermediate

How to upload file via FTP in Perl?

Question

How to upload file via FTP in Perl?

Uploading a file via FTP in Perl is typically done with the Net::FTP module, which is a core module distributed with Perl. It offers a simple, object-oriented API to connect, login, and transfer files using the FTP protocol.

However, since a working FTP server and accessible local files are required, the example below is a *sandbox-friendly simulation* that demonstrates the correct method calls and error handling without actual network or file operations. This version prints the intended actions instead of performing them, making it safe and runnable anywhere.

Key Concepts

  • Net::FTP: Core Perl module for FTP tasks.
  • Sigils: Scalars ($ftp) hold object references.
  • Context: Methods return success/failure, usually checked with or die.
  • TMTOWTDI: You can use alternative FTP implementations or system calls, but Net::FTP is standard.

Sandbox-safe Example (No Real Connection or File Access)

use strict;
use warnings;

{
  package FakeFTP;
  # Simulate Net::FTP methods for sandbox demo
  sub new {
    my ($class, $server, %opts) = @_;
    print "Connecting to FTP server '$server' with options: ",
          join(", ", map { "\$_=$_"} keys %opts), "\n";
    return bless {}, $class;
  }
  sub login {
    my ($self, $user, $pass) = @_;
    print "Logging in as user '$user'\n";
    return 1;  # success
  }
  sub put {
    my ($self, $local, $remote) = @_;
    print "Uploading local file '$local' as remote file '$remote'\n";
    return 1;  # success
  }
  sub quit {
    print "Closing FTP connection\n";
    return 1;
  }
}

# Replace Net::FTP with our FakeFTP for demo purposes
my $ftp_server  = 'ftp.example.com';
my $username    = 'username';
my $password    = 'password';
my $local_file  = 'upload.txt';
my $remote_file = 'remote_upload.txt';

# Use FakeFTP->new instead of Net::FTP->new
my $ftp = FakeFTP->new($ftp_server, Debug => 0, Passive => 1)
  or die "Cannot connect to $ftp_server";

$ftp->login($username, $password)
  or die "Cannot login";

$ftp->put($local_file, $remote_file)
  or die "Upload failed";

print "File '$local_file' uploaded successfully as '$remote_file'\n";

$ftp->quit;

Explanation and Tips

  • The key FTP methods are new (connect), login (authenticate), put (upload), and quit (close connection).
  • Always check return values for success; otherwise, use or die with $ftp->message to diagnose.
  • Passive mode (Passive => 1) is often required behind firewalls.
  • For real file transfers, ensure the local file exists and FTP server details are correct.
  • FTP is unencrypted; for sensitive data, consider SFTP or FTPS instead.

Common Pitfalls

  • Trying to connect to placeholder hosts like ftp.example.com results in DNS or connection errors.
  • Uploading files that don't exist locally causes silent failures; validate local files first.
  • For binary files, use $ftp->binary() before uploading to avoid corruption.

By using the standard Net::FTP interface shown, your FTP upload code will work on Perl 5.6+ with no external dependencies. Just swap in your actual server/user/file details when ready.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 9ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Connecting to FTP server 'ftp.example.com' with options: $_=Passive, $_=Debug
Logging in as user 'username'
Uploading local file 'upload.txt' as remote file 'remote_upload.txt'
File 'upload.txt' uploaded successfully as 'remote_upload.txt'
Closing FTP connection
STDERR
(empty)

Was this helpful?

Related Questions