http://qs1969.pair.com?node_id=304627

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

Hi!
I'm trying to ftp some files from a remote server using the net::ftp module. It was working fine all these days, when I did a get|put etc.
Recently, while I was performing an ftp of a zip file of size above 4000 kb, I get an error while opening it, which says "....69 bytes missing....". I have no idea why.
Here's my script:
#!/usr/bin/perl -w use strict; use Net::FTP; use Cwd; $|++; my ($ftpmessage, $ftp, $sourcefile, $option, $remote_path); my ($pwdr, $logfile); my %parm = ( ftphost => '255.255.255.255', ftpuser => 'user', ftppass => 'passwd', ); sub get_the_file($) { my $source=shift; $ftp->get("$source") or LogIt('exit', "Error uploading.\nDisk space or permissions probl +ems?\n"); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); } sub get_the_pwd { my $fullpath=$ftp->pwd(); print "Fullpathname $fullpath\n"; $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); } sub set_the_pwd($) { my $directory=shift; $ftp->cwd("$directory") or LogIt('exit', "Could not change the directory to $directory! \ +n"); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); } # Print message with date+timestamp to logfile. # Abort program if instructed to. sub LogIt { my $exit = $_[0]; my $msg = $_[1]; print LOG "$msg"; #my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time); print LOG scalar localtime(), "\n"; #$year+1900,"-",$mon+1,"$mday $hour:$min:$sec\n"; exit if ($exit eq 'exit' ); } { $pwdr=cwd;; chomp($pwdr); if ( $#ARGV == -1 ) { print "$0:Usage $0 get\|put\|list path filename\n"; exit; } elsif ($ARGV[0] !~ /(get)|(put)|(list)/i) { print "$0:Usage $0 get\|put\|list path filename\n"; exit; } $option=$ARGV[0]; $remote_path=$ARGV[1]; $sourcefile=$ARGV[2]; if ( -e "$pwdr\\FTPLogs") { print "Writing Log file in $pwdr\\FTPL +ogs...\n"; } else { mkdir("$pwdr\\FTPLogs",0777) || die "Sorry, I couldn't crea +te $pwdr\\FTPLogs folder $!\n"; } ($main::sec,$main::min,$main::hour,$main::mday,$main::mon,$main::y +ear,,,) = localtime(time); $main::mon+=1; $main::year+=1900; $logfile="$pwdr\\FTPLogs\\FTP_log_$main::mday\_$main::mon\_$main:: +year\_$main::hour\_$main::min\_$main::sec"; open (LOG, ">>$logfile") or die "Error opening $logfile: Reason$! +"; LogIt('noexit', "\n\n********************************************* +***************\n Started run\n"); print LOG " Destination server $parm{ftphost} Destination user $parm{ftpuser} Log file $logfile Upload script $0 Perl $] Net::FTP $Net::FTP::VERSION Local OS $^O\n", ; if ((defined $sourcefile) && ($option =~ /put/i)) { unless (-r $sourcefile && -T $sourcefile) { LogIt('exit', "Error with source file $sourcefile.\nIs not readable or +ASCII.\n" ); } } $ftp = Net::FTP->new($parm{ftphost}) or LogIt('exit', "Error connecting.\nNetwork or server problem?\n") +; $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); $ftp->login($parm{ftpuser}, $parm{ftppass}) or LogIt('exit', "Error logging in.\nHas ID or password changed?\n" +); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); $ftp->ascii(); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); set_the_pwd("$remote_path"); get_the_pwd(); if ($option =~ /put/i) { put_the_file("$sourcefile"); } elsif ($option =~ /get/i) { get_the_file("$sourcefile"); } else { get_file_list(); } $ftp->quit() or LogIt('exit', "Error disconnecting.\n???\­n"); $ftpmessage=$ftp->message(); LogIt('FTP Says:', "$ftpmessage\n"); close LOG or die "Error closing $sourcefile: Reason$!"; }
Please let me know in case I have to make any changes or anything.
Thanks!
SRK.

Edit, BazB. Add readmore, change title (was "Net::FTP").

Replies are listed 'Best First'.
Re: File corrupt when using Net::FTP
by Roger (Parson) on Nov 05, 2003 at 05:29 UTC
    It sounds suspicious when you have $ftp->ascii(); turned on (and not turned off) while transfering a binary ZIP file. Have you tried to stick a $ftp->binary(); in your subs get_the_file and put_the_file?

      This is exactly the kind of error this can couse. The ascii mode on Windows systems would interpret ^Z as an End Of File.
Re: File corrupt when using Net::FTP
by vek (Prior) on Nov 06, 2003 at 00:41 UTC

    Setting binary mode $ftp->binary; should do the trick.

    -- vek --
Re: File corrupt when using Net::FTP
by ehdonhon (Curate) on Nov 05, 2003 at 06:07 UTC

    I would have front-paged this question, but you seem to have forgotten to add a <readmore> tag to your post.