Solution to Transfer files by php ftp_put() : warning: ftp_put() [function.ftp-put]: Opening data connection

Posted by: 
Dominique De Cooman

The whole thing did not work without this line.

ftp_pasv($conn_id, true);

Apparently with some ftp accounts you need to transfer in passive mode. As usual not documented by the client.

The full excerpt:

<?php
function exporter_vdab_ftp($file_path,$file_name) {
  
// set up basic connection
  
$file $file_path '/'.$file_name;
  
$ftp_server VDAB_FTP_SERVER;
  
$conn_id ftp_connect($ftp_server);

  
// login with username and password
  
$ftp_user_name VDAB_FTP_USER;
  
$ftp_user_pass VDAB_FTP_PASS;
  
$login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

  
// check connection
  
if ((!$conn_id) || (!$login_result)) {
    print 
"FTP connection has failed!";
    print 
"Attempted to connect to $ftp_server for user $ftp_user_name";
    return 
false;
  }
  else {
     print 
"Connected to $ftp_server, for user $ftp_user_name";
  }
  
  
//turn passive mode on
  
ftp_pasv($conn_idtrue);

  
// upload the file
  
$upload ftp_put($conn_idVDAB_REMOTE_PATH '/' $file_name$fileFTP_BINARY);

  
// close the FTP stream
  
ftp_close($conn_id);
}
?>

Add new comment