PDA

View Full Version : Need help with Net::FTP


grandfso
09-04-2006, 04:37 PM
Hello,
I am newbie to this forums, and newb to perl/cgi as well.
I would like to get a CGI script, that retrieves files from remote server, and 'downloads' these files to a webserver, that runs the script.
How Is that possible? I did some googling, and I know I need to use net:ftp, but well... the truth is I can't!

FishMonger
09-04-2006, 05:52 PM
I need to use net:ftp, but well... the truth is I can't!
Why can't you?

Have you read the documentation for the Net::FTP module?
http://search.cpan.org/~gbarr/libnet-1.19/Net/FTP.pm

Have you tried writting a test script and if so, what does your script look like?

What error messages are you getting?

rwedge
09-06-2006, 05:18 AM
If you are fetching published , non password protected files, LWP::Simple or LWP::UserAgent would do.

Rakish
09-06-2006, 07:16 AM
Here is the script that uploads a file from webserver to an FTP server using Net::FTP,

#!/usr/bin/perl

use CGI;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use Net::FTP;

my $q = new CGI;

print "Content-Type: text/html\n\n";

if($q->param("File"))
{
#
my $h = $q->upload("File");
open UP,">/tmp/$h";
binmode UP;
while(<$h>)
{
print UP;
}
close UP;

$ftp = Net::FTP->new("ftpserver.com", Debug => 0) or die "Cannot connect to ftpserver.com: $@"; # connect
$ftp->login("ftpuser",'ftppassword') or die "Cannot login ", $ftp->message; # login
$ftp->cwd("/pub") or die "Cannot change working directory ", $ftp->message; # change dir if required
$ftp->binary(); # change the mode to binary
$ftp->put("/tmp/$h","$h"); # upload the file
$ftp->quit(); # close the control connection


}
else
{
print<<HTML;
<form method="post" enctype="multipart/form-data">
<input type="file" name="File">

<input type="submit">
</form>
HTML

}


to retrieve a file from an FTP server to a webserver replace:

$ftp->put("/tmp/$h","$h"); # upload the file

to

$ftp->get("filename.extension"); # downloads the file to the current directory.

for more information please refer:

http://search.cpan.org/~gbarr/libnet-1.19/Net/FTP.pm


-Rakesh