FTPs a single file to an ftp server, you specify the remote location. ./sftp ~/pic.jpg /www/public Only just starting out in perl... needs Net::FTP
#!/usr/bin/perl #---------------------------- #FTP's single file to servage #By Benjamin Ruston #benjamin@digitalwombat.com #Ponder@DALnet #28th November 2006 #------ #Usage: ./ftpsend ~/dog.jpg /www/public #./ftpsend filename remotelocation #------ use Net::FTP; $file = $ARGV[0]; $rloc = $ARGV[1]; $host = "ftp.host.here"; $ftp = Net::FTP->new($host, Debug => 0); #Login and password need to be set here $ftp->login("username",'password'); $ftp->cwd($rloc); $ftp->put($file); $ftp->quit;

Replies are listed 'Best First'.
Re: FTP single file to remote server
by marto (Cardinal) on Nov 28, 2006 at 13:50 UTC
    ponder,

    I understand that you are just starting out with Perl (perhaps this is your first programming experience). One thing I would like to point out (zentara has already made you aware of potential confusion caused by the name of your script) is that you are not checking for failures, and assuming everything worked fine. Take a look at this slight reworking of your code:
    #!/usr/bin/perl #---------------------------- #FTP's single file to servage #By Benjamin Ruston #benjamin@digitalwombat.com #Ponder@DALnet #28th November 2006 #------ #Usage: ./sftp ~/dog.jpg /www/public #./sftp filename remotelocation #------ use strict; use warnings; use Net::FTP; my $file = $ARGV[0]; my $rloc = $ARGV[1]; my $host = "ftp.host.here"; my $ftp = Net::FTP->new($host, Debug => 0) or die "Could not connect t +o host $host : $@" #Login and password need to be set here $ftp->login("username",'password') or die "Failed to log in ", $ftp->m +essage; $ftp->cwd($rloc) or die "Failed change working directory ", $ftp->mess +age; $ftp->put($file) or die "Failed to transfer file ", $ftp->message; $ftp->quit;

    Note the addition of the use strict; use warnings; about the use Net::FTP line. For more info on why I added these lines read Use strict and warnings from the Tutorials section of this site.

    Also note that I had added lines to display a message when an operation failed. In the event of a failure, your code would look to the user as though it had executed without any problems. Note that I have printed a message to the screen at each point the FTP operation could fail. The examples in the Net::FTP documentation inform you how to do this. The Tutorials section of this site is a great resource, and always read the module documentation.

    Hope this helps.

    Martin

    Update: Fixed a couple of typos
Re: FTP single file to remote server
by zentara (Cardinal) on Nov 28, 2006 at 13:32 UTC
    Only just starting out in perl

    You might want to change the name; sftp is an existing widely used program, and your name w(c)ould cause confusion in the shell's path.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Thanks.
        ponder,

        I noticed you have taken the advice you have been given regards the name, and changed your code. In order to avoid confusion when people make a change to something they have posted they (generally) add a line to state what has been udpated. For example:

        Update: Changed program name from sftp to ftpsend.

        Take a look at the PerlMonks FAQ if you have not already done so.

        Thanks

        Martin