FTP file upload using perl from local system

14,715

You can try this code this is working fine for me

use strict;
use warnings;
use Net::FTP;

my ($ftp, $host, $user, $pass, $dir, $fpath);

$host = "";
$user = "";
$pass = "";
$dir = "";

$fpath = "";

$ftp = Net::FTP->new($host, Debug => 0);
$ftp->login($user, $pass) || die $ftp->message;
$ftp->cwd($dir);
$ftp->put($fpath) || die $ftp->message;
$ftp->quit;

print $ftp->message;
Share:
14,715
arka.b
Author by

arka.b

Updated on November 24, 2022

Comments

  • arka.b
    arka.b over 1 year

    Requirement:

    I want to create a script that will upload files from local system to FTP server in using perl.

    Sample Code:

    use Net::FTP;
        $ftp = Net::FTP->new("some.host.name", Debug => 0)
          or die "Cannot connect to some.host.name: $@";
        $ftp->login("anonymous",'-anonymous@')
          or die "Cannot login ", $ftp->message;
        $ftp->cwd("/pub")
          or die "Cannot change working directory ", $ftp->message;
        $ftp->get("that.file")
          or die "get failed ", $ftp->message;
        $ftp->quit;
    

    P.S: Newbie in Perl.

    • Quentin
      Quentin over 10 years
      Define "not working". What does it do? What error messages are reported?
    • Admin
      Admin over 10 years
      it is not posting the any files
    • Quentin
      Quentin over 10 years
      What is it doing? What error messages are reported? What makes you think it should "post" files (whatever you mean by that)?
  • Developer
    Developer over 10 years
    what is the problem in my code will u explain why u have downvoted it
  • Admin
    Admin over 10 years
    file path i have to pass is relative or absolute
  • Developer
    Developer over 10 years
    pass the complete path from both locations
  • arka.b
    arka.b over 8 years
    this serves only uploading to FTP server. How about downloading the same file now? Another requirement is now I want to repeat this upload/download process inside a loop. How to achieve it?
  • arka.b
    arka.b over 8 years
    where do the downloaded files gets saved?
  • Developer
    Developer over 8 years
    set your location where you want
  • arka.b
    arka.b over 8 years
    I added $sock->recv("/home/rancore/arka/10mega.test") or die $ftp->message; and installed the module IO:Socket:Socks:Wrapper. It did not work.
  • arka.b
    arka.b over 8 years
    Gaurav - Can you look into this one? stackoverflow.com/questions/32822411/…