开发者

How can I resume an upload to ftp from php

I have a p开发者_开发技巧hp-script that uploads files to an ftp server from a location with a very low bandwith, low reliability connection. I am currently using the ftp functions of php.

Sometimes the connection drops in the middle of a transfer. Is there a way to later resume the upload? How can this be done?

Edit: People misunderstood that this is happening from a browser. However, this is not the case. It is a php cli script. So it is about a local file being uploaded to ftp, no user interaction.


Try getting the remote file's size and then issuing the APPE ftp command with the difference. This will append to the file. See http://webmasterworld.com/forum88/4703.htm for an example. If you want real control over this, I recommend using PHP's cURL functions.


I think all the previous answers are wrong. The PHP manage the resume, for that FTP_AUTODESK must be activated, and you have to activate the FTP_AUTORESUME during upload. The code should be something like this :

$ftp_connect = ftp_connect($host, $port);
ftp_set_option ($ftp_connect, FTP_AUTOSEEK, TRUE);
$stream = fopen($local_file, 'r');
$upload = ftp_fput($ftp_connect, $file_on_ftp, $stream, FTP_BINARY, FTP_AUTORESUME);
fclose($stream);


ftp_(f)put has a $startpos parameter. You should be able to do what you need using this parameter. (starts the transfer from the file size on your server). However I never used it, so I don't know about the reliability. You should try.


EDIT: Another Example: by cballou

Look here. A very simple and good example.

You could do something like this:

<?php
     $host = 'your.host.name';
     $user = 'user';
     $passwd = 'yourpasswd';


     $ftp_stream = ftp_connect($host);
     //EDIT
     ftp_set_option($ftp_stream, FTP_AUTOSEEK, 1);
     if ( ftp_login($ftp_stream,$user,$passwd) ){
        //activate passive mode
        //ftp_pasv($ftp_stream, TRUE);

        $ret = ftp_nb_put($ftp_stream, $remotefile, $localfile, FTP_BINARY,FTP_AUTORESUME);
        while ( FTP_MOREDATA == $ret ) {
            // continue transfer
            $ret = ftp_nb_continue($ftp_stream);
        }

        if (FTP_FINISHED !== $ret){
           echo 'Failure occured on transfer ...';
        } 
     }
     else {
         print "FAILURE while login ...";

     }

     //free
     ftp_close($ftp_stream);
?>


are you meaning you are going to allow user resume upload from you web interface ? is this suit you ? http://www.webmasterworld.com/php/3931706.htm


just use the filezilla can done everything for you ...i mean the uploading process... it will keep the data and reopen after close you will be able to continue process q...

for automate and tracking , simply use our way...which is git and capistrano there is an option for php try google it...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜