Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use NET::FTP to transfer files from a cgi script on a webserver to a ftp server for archiving. The end user will upload a PDF via the cgi script, then it will be sent to the ftp server. For beurocratic reasons these machines must be seperate, and I don't have direct control over the webserver.

The problem is that NET::FTP truncates every file I try to send this way. The file makes it intact to the webserver, but it is truncated when it arrives at the ftp server. Using standard unix ftp I have no problems, however I have not found a way to use anything other than NET::FTP in a perl script. ( scp won't work for anonymous xfers, which I need ). Ascii and binary modes have the same effect as both platforms are running *nix variants ( I've also tested it ).

The relevnant code is:
$ftp->login('anonymous','submit@somesrvr.that.exists.edu'); $ftp->put("$prefix"."$f2"); $ftp->quit;

where "$prefix"."$f2" expands to the local file on the webserver, as the cgi script writes the uploaded file to a temp location ( named $prefix$f2 ) then sends that to the ftp server.

Could anyone point me in the right direction? I've been grepping and hitting google/monks/perl.com/lugs.. etc. for a while now with no luck.

Thanks!
Rogan Creswick
Rogan.Creswick@orst.edu

Replies are listed 'Best First'.
Re: NET::FTP truncates files
by Dr. Mu (Hermit) on Aug 09, 2001 at 08:53 UTC
    Your CGI program may have received the whole file, but did you remember to close it before doing the ftp? If not, you may have a buffering problem. The put is sending everything that exists on the disk, but there may be more in memory that hasn't been written out yet. When your program terminates, the file will be closed, and that last buffer will be written. So when you go to examine the file on the server, it'll be complete.
      that was it!

      I can't believe I didn't close it. Thanks for the help and ideas, to everyone. All I needed was close(FILE); before the ftp code :)

      Rogan Creswick
Re: NET::FTP truncates files
by Agermain (Scribe) on Aug 09, 2001 at 00:57 UTC
    I'm not sure if Net:FTP automatically checks, but maybe it's sending the file in ASCII/text mode instead of binary? To do this you would just send a $ftp->binary(); command. This sounds like the problem, since a text transfer will end when you get an EOF character - of which a PDF can't guarantee /won't/ show up before the end of the file.

    andre germain
    "Wherever you go, there you are."

      I have tried this
      $ftp->login('anonymous','submit@somesrvr.that.exists.edu'); $ftp->binary(); $ftp->put("$prefix"."$f2"); $ftp->quit;
      with the same result..

      Rogan Creswick ( didn't have an account earlier )
Re: NET::FTP truncates files
by lemming (Priest) on Aug 09, 2001 at 01:37 UTC

    Put $ftp->binary() before the transfer. From the tests I ran on Linux <-> Linux it would default to an ascii transfer. And running ftp direct, defaulted to binary.

    update: Hmm. A few questions/observations

  • I did notice that CPAN uses Net::FTP. Not sure if that helps you at all, but may make an OK test
  • It wasn't clear if you tested only the ftp code.
  • Are you sure the file transfer has completed on the webserver before the ftp transfer happens? That could explain the truncation.
Re: NET::FTP truncates files
by princepawn (Parson) on Aug 09, 2001 at 02:21 UTC