PHP - Transfering a file from a ftp server to another

Discussion in 'PHP' started by mc2fred, Apr 14, 2010.

  1. #1
    Hello everyone,

    I'm in need of a php code that will transfer large video files from a server to another.

    The starting file will be located on the root of the first server and transferred to the root of the second folder

    I've looking all around for a simple php solution using ftp functions.

    I built this piece of code and for some unknown reason, it keeps returning "There was an error downloading the file..."

    Any idea why it's not working or do you have better suggestions for the solving of this problem ?

    Thank you !
    -Fred

    <?php
     $ftp_server="server_a.com";
     $ftp_user_name="name1";
     $ftp_user_pass="pass1";
     
     $fichier='video_name.flv';
      
     $conn_id = ftp_connect($ftp_server)or die("Impossible to connect to server A");
     $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die ("Impossible to login on server A");
     ftp_pasv($conn_id,true);
     
     $ftp_server2="server_b.com";
     $ftp_user_name2="name2";
     $ftp_user_pass2="pass2";
     
     $conn_id2 = ftp_connect($ftp_server2)or die("Impossible to connect to server B");
     $login_result2 = ftp_login($conn_id2, $ftp_user_name2, $ftp_user_pass2) or die ("Impossible to login on server B");
     ftp_pasv($conn_id2,true);
     
     $ret=ftp_nb_fget($conn_id, ($_SERVER['DOCUMENT_ROOT'].$fichier), $new, FTP_BINARY);
    	while ($ret == FTP_MOREDATA) {
    
    	$ret2 = ftp_nb_fput($conn_id2, $fichier, $new, FTP_BINARY);
    	while ($ret2 == FTP_MOREDATA) {
    
       echo ".";
    
       $ret2 = ftp_nb_continue($conn_id2);
    }
    if ($ret != FTP_FINISHED) {
       echo "There was an error uploading the file...";
       exit(1);
    }
    	
       $ret = ftp_nb_continue($conn_id);
    }
    if ($ret != FTP_FINISHED) {
       echo "There was an error downloading the file...";
       exit(1);
    }
    
     ftp_close($conn_id);
     ?>
    PHP:
     
    mc2fred, Apr 14, 2010 IP
  2. mc2fred

    mc2fred Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I replaced the bottom functions with ftp_raw functions and it worked !

    Unfortunatly, I cant find the file on the receiving server T.T

    Here's the final code :

    ===Top Part===

    
     $ftp_server="server_a.com";
     $ftp_user_name="name1";
     $ftp_user_pass="pass1";
     
     $fichier='video_name.flv';
      
     $conn_id = ftp_connect($ftp_server)or die("Impossible to connect to server A");
     $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die ("Impossible to login on server A");
     ftp_pasv($conn_id,true);
     
     $ftp_server2="server_b.com";
     $ftp_user_name2="name2";
     $ftp_user_pass2="pass2";
     
     $conn_id2 = ftp_connect($ftp_server2)or die("Impossible to connect to server B");
     $login_result2 = ftp_login($conn_id2, $ftp_user_name2, $ftp_user_pass2) or die ("Impossible to login on server B");
     ftp_pasv($conn_id2,true);
    PHP:
    ===New Part===

    $ansver = ftp_raw($conn_id, 'PASV');
    
    if (intval($ansver[0]) == 227) {
        ftp_raw($conn_id2, 'PORT '.substr($ansver[0], $n = strpos($ansver[0], '(') + 1, strpos($m[0], ')', $n) - $n));
    	
        ftp_raw($conn_id, 'STOR '.$fichier); // need asynchronously (non-blocking)
        ftp_raw($conn_id2, 'RETR '.$fichier);
    }
    
     ftp_close($conn_id);
    PHP:
    Thank you :D
     
    mc2fred, Apr 14, 2010 IP