in reply to Re: How Do I Use Net::FTP
in thread How Do I Use Net::FTP

Dave, Is this something that would work?
my $filehandle = "C:/Temp"; $ftp->get("00000001.610,$filehandle);
Am I even close? Thanks

Replies are listed 'Best First'.
Re^3: How Do I Use Net::FTP
by davido (Cardinal) on May 17, 2005 at 15:57 UTC

    No, you should have read the docs on open, and probably perlopentut.

    What I suggested above is that you open a file for output, and use its filehandle. You do that like this:

    $ftp->binary; open my $filehandle, '>', 'c:/Temp/00000001.610' or die "Couldn't open output file.\n$!"; binmode $filehandle; $ftp->get("00000001.610", $filehandle); close $filehandle or die 'Couldn't close output file.\n$!"; $ftp->quit;

    That is untested, so use at your own risk. Also, eliminate the $ftp->binary; line, and the 'binmode $filehandle;' line if you are dealing with text files.


    Dave

      Dave, At least no more errors are popping up. Here is my code:
      #!/usr/local/bin/perl -w use Net::FTP; use Sys::Hostname; $hostname = hostname(); $hostname = '*****.com'; # ftp host $username = '*******'; # username $password = '*******'; # password $home = '/doytest/cgi-bin/Data/*.txt'; $ftp = Net::FTP->new($hostname); # Net::FTP constr +uctor $ftp->login($username, $password); # log in w/userna +me and password $pwd = $ftp->pwd; # get current dir +ectory open my $filehandle, '>', 'c:/Temp/00000001.610' or die "Couldn't open output file.\n$!"; $ftp->get("00000001.txt", $filehandle); close $filehandle or die "Couldn't close output file.\n$!"; # Now, output HTML page. print <<HTML; Content-type: text/html <HTML> <HEAD> <TITLE>Download Files</TITLE> </HEAD> <BODY> <B>Current working directory:</B> $pwd<BR> Files to download: <P> HTML @entries = $ftp->ls($home); # slurp all entr +ies into an array print "@entries \n\n"; print "$filehandle\n"; print <<HTML; </BODY> </HTML> HTML $ftp->quit;
      The file does not copy into my Temp folder on my c: drive. Possible answers? Thanks

        Is this a CGI script? Check your server's error log. You'll probably find an answer there.

        Also, change your $ftp->get(.....) line as follows:

        $ftp->get("00000001.txt", $filehandle) or die "Couldn't get 00000001.txt\n$!";

        Then run the script again, and check your error logs. Also, if this is a CGI script, use CGI::Carp with the fatalsToBrowser setting. See the documentation for CGI::Carp at the link provided in this paragraph. Use it as a development tool, and then remove it once things are working as they should.

        Most likely problems could include a problem getting the file, or a problem opening for output.


        Dave

        If this is a CGI-type script (as it appears to be), then it is running on the server. If it is running on the server, then Net::FTP cannot save a file to your machine, only the machine where it actually executes.

        This is different from how a Java applet (for example) operates, by executing on your local machine. Perl scripts accessed in CGI mode execute only on the server.

        My apologies if I misunderstood your question.


        The Eightfold Path: 'use warnings;', 'use strict;', 'use diagnostics;', perltidy, CGI or CGI::Simple, try the CPAN first, big modules and small scripts, test first.