http://qs321.pair.com?node_id=526331

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

Hi people,
I am using the Net::FTP module to transfer user files from a browser. Here is the subroutine that transfers the file
sub ftpTransfer { # connect my $ftp = Net::FTP -> new($server); print $ftp -> message(), "\n"; # login $ftp -> login("$user","$pass"); print $ftp -> message(), "\n"; # set binary mode $ftp -> binary(); print $ftp -> message(), "\n"; # move through directories # $dir = "ftp"; # $ftp -> cwd($dir); # print $ftp -> message(), "\n"; # list files my @list = $ftp -> ls(); print $ftp -> message(), "\n"; print "<br>Filename: $filename<br>"; # put files on the server $ftp->put($filename, $filename) or die "could not put $file"; print $ftp -> message(), "\n"; $ftp -> quit(); }
What i am wanting to do is show a progress bar. I am aware of the need to flush the output buffer. How can I use the 'put' command in a loop that will allow me to display a progress indication to inform the user on the time remaining to transfer. This interface will be used to transfer large files upto 100mb.
Thanks for any help.

Replies are listed 'Best First'.
Re: FTP transfer progress
by rhesa (Vicar) on Jan 30, 2006 at 01:48 UTC
    You won't be able to access progress data by using the put() method, since that's a one-shot method. What you need to do is emulate what put() does. You can do that by using the stor() method, and using the write() method on the resulting Net::FTP::dataconn object.

    I don't have a complete solution for you, but the following should get you started:

    # advertise that we want to store a file. # $filename should probably not contain a path my $conn = $ftp->stor( $filename ); # open the local file if( open my $fh, $filename ) { my $buf; while(my $read_bytes = read($fh, $buf, 1024) > 0) { $conn->write($buf, $read_bytes); # add your progressbar update here } }
Re: FTP transfer progress
by sk (Curate) on Jan 30, 2006 at 01:39 UTC
Re: FTP transfer progress
by zentara (Archbishop) on Jan 30, 2006 at 12:41 UTC
    If you are looking for more options, curl and libcurl, have built in ways to setup callbacks, so you can show progress. They are a bit tricky to use though.

    I'm not really a human, but I play one on earth. flash japh