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

Dear monks
I have an issue with my boss I am working on a program that sends text file by FTP, and he constantly is saying that : What happens when the connection is lost and the file on the server is zero???, I really never seen something like that, so he continues to say that I need to use sockets, which in my poor life as a programmer sounds creepy and rare, does someone knows how to verify that the file I am sending by ftp gets to the server complete? I thought on the size of the file but is that posible??? again this is part of what I have done so far: this is the part where I send the file by FTP:
my @fileList = glob "m*.*"; foreach my $file (@fileList) { chomp($file); if ( $file =~ /.+\.\d{1,}/ ) { # one or more characters, a '.' foll +owed by one or more digits print STDOUT "<Nombre valido del Archivo> " . $file . "\n"; my $server = "xxx.xx.xx.xx"; my $username = "xxxxx"; my $pass = "xxxxxxxxxx"; my $ftp; print "Connecting to $server.."; # Set up connection $ftp = Net::FTP->new( $server, Passive => 1, Debug => 0 ) o +r die $@; print "..authenticating.."; # Log in... $ftp->login( $username, $pass ) or die $ftp->message; print "..done!\n"; $ftp->cwd('Envio') or die $ftp->message; print $ftp->pwd (), "\n"; $ftp->ascii(); $ftp->put("$file") or die $ftp->message; print "Logging out.."; #or die $ftp->message; $ftp->quit; print "..done!\n"; } else { print STDOUT "<Nombre Invalido del Archivo> " . $file . "\n"; } } for (@transferFileList) { print "$_\n"; system ("del",$_); } }

Replies are listed 'Best First'.
Re: How to make sure a file send by ftp get to the server complete???
by NetWallah (Canon) on Apr 17, 2008 at 03:18 UTC
    Net::FTP provides the size ( FILE ) method - after transfer, you can query the file size, and compare to the local host.

    Please note that it is possible to have documented variations if the receiving machine's idea of "End-of-line" is different than the senders (Windows vs Linux), and the file is sent ASCII.

    You can also inform jefe that FTP does, indeed use sockets, under the hood.

         "How many times do I have to tell you again and again .. not to be repetitive?"

Re: How to make sure a file send by ftp get to the server complete??? (rename after)
by tye (Sage) on Apr 17, 2008 at 03:36 UTC

    I would never do automated transfers of files via FTP without following this "best practice":

    Transfer the file to a temporary name. After the sending side has been told that the file was sent successfully, the sending side uses FTP to rename the remote file from the temporary name to the final name.

    - tye        

      By doing this you also prevent any scheduled processing on the other side from processing a partial file, ie if the transfer speed was unusually slow or the file size was unusually big.
Re: How to make sure a file send by ftp get to the server complete???
by swampyankee (Parson) on Apr 17, 2008 at 01:05 UTC

    He is your boss, so it wouldn't do to antagonize him too much ;-).

    First test: make sure that the file has the same size on the receiving and transmitting end.

    Second, and tougher test: try to get a check sum on the receiving end.

    Third, and most wasteful of bandwidth: copy the file back to the transmitting machine, with a new name, and compare the two files.

    addendum:  I have seen file transfers via ftp fail, so I would not assume it can't happen.


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Re: How to make sure a file send by ftp get to the server complete???
by poolpi (Hermit) on Apr 17, 2008 at 06:35 UTC

    I think you can throw an exception and retry with something like Sub::Attempts
    or you can have a look at Net::FTP::AutoReconnect

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
Re: How to make sure a file send by ftp get to the server complete???
by salva (Canon) on Apr 17, 2008 at 09:45 UTC
    Net::FTP should be able to detect failed or incomplete transfers and report them accordingly. Though, using the size method to check that both remote and local files have the same length will not harm!

    Another option is to use a more sophisticated FTP client like lftp, able to handle several simultaneous connections and recover from network problems.

Re: How to make sure a file send by ftp get to the server complete???
by apl (Monsignor) on Apr 17, 2008 at 11:56 UTC