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

I have created a script that will ftp a file to a number of different servers. The problem I am having is the file is being transferred and the file size remains the same but the file looks empty. All the characters in the file are being replaced with NULL values.
sub ftp_file{ my ( $hosts ) = @_; my ($pass, $remotefile); my @hosts = (); my %host = (); my $currdir = cwd(); my $localfile = sprintf("%s/%s", $params->get("Path.From"), $params->get("File")); if ($params->exists("Path.To")){ $remotefile = sprintf("%s/%s", $params->get("Path.To"), $params->get("File")); }#end if statement else{ $remotefile = sprintf("%s/%s", $params->get("Path.From"), $params->get("File")); }#end else statement &ebenx::nm_UnHide($pass, $hosts->[2]); my $host = { 'host' => $hosts->[0], 'login' => $hosts->[1], 'pass' => $pass }; $app->log($INFO, "Copying [ $localfile ] to $$host{host}"); my $ftp = new Net::FTP($$host{host}, Timeout => 30) or $app->log($FATAL, "Unable to initialize ftp object: $!"); $ftp->login($$host{login}, $$host{pass} ) or $app->log($FATAL, "Unable to login to $$host{host}: $!"); $ftp->put($localfile, $remotefile) or $app->log($FATAL, "Unable to put $localfile to $$host{host} + as $remotefile: $!"); $ftp->quit(); }#end ftp_file

Replies are listed 'Best First'.
Re: NET::FTP replaces characters in file with NULL
by Improv (Pilgrim) on Apr 22, 2003 at 17:05 UTC
    I have been unable to replicate your problem -- your code works fine for me (with all the CGI stuff cleaned out, anyhow). Are you sure the original file isn't empty? Is that file uploaded to the CGI, which then sends it on? If so, perhaps there's a bug elsewhere in your program. Note that I'm assuming it's a cgi because of the calls to param(). I can see that you're also new to perl, and probably came from C. Allow me to suggest that string handling doesn't need to be as complex as you make it out to be. Compare
    my $localfile = sprintf("%s/%s", $params->get("Path.From"), $params->get("File"));

    to
    my $localfile = params->get("Path.From") . '/' . $params->get("File");

    You also are using more than one variable with the same name, which can be confusing. I suggest you examine your usage of @hosts and $hosts in your program. Without the cleanup I did to make it not require CGI, I doubt your program would do what you'd expect it to. Below are some more comments.. I hope this helps.
      You also are using more than one variable with the same name, which can be confusing. I suggest you examine your usage of @hosts and $hosts in your program.

      Personally I have no problem with using %hosts, @hosts and $hosts all in the same code. But thats just me. Now if @hosts contained something other than hosts, I would find that confusing. ;-) (Although I personally would lean towards %host @host and $host)


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
      no I am not using CGI. Some of our programs use it and some don't and in order to maintain some consistency we use the same configuration files we load the same files. I am sure that the file starts with data. Because one of the sub routines copies that file to make sure we don't loose it and the copied file has data. I also made sure that the cp() wasn't problem.
Re: NET::FTP replaces characters in file with NULL
by graff (Chancellor) on Apr 23, 2003 at 03:36 UTC
    the file size remains the same but the file looks empty. All the characters in the file are being replaced with NULL values

    Two questions:

    • When was the last time you tried using a common command line ftp utility to replicate what the perl script is doing, to see whether it was just the perl script, or just the ftp connection?
    • When you say "the file looks empty...replaced with NULL values", it's not entirely clear whether this means the contents are all null bytes, or whitespace, or other "invisible" character codes. Have you done a hex dump of the file content (e.g. using "od") to see what the delivered byte values really are?