Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

FTP transfer progress

by diggernz (Sexton)
on Jan 30, 2006 at 00:17 UTC ( #526331=perlquestion: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://526331]
Approved by McDarren
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-12-09 01:11 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (36 votes). Check out past polls.