Dominique De Cooman
Published on Dominique De Cooman (https://dominiquedecooman.com)

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

Dominique De Cooman
Wednesday, June 24, 2009 - 22:58
Php [1]
ftp [2]

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_id, true);

  
// upload the file
  
$upload = ftp_put($conn_id, VDAB_REMOTE_PATH . '/' . $file_name, $file, FTP_BINARY);

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


Source URL: https://dominiquedecooman.com/blog/solution-transfer-files-php-ftpput-warning-ftpput-functionftp-put-opening-data-connection

Links
[1] https://dominiquedecooman.com/blog-topics/php
[2] https://dominiquedecooman.com/blog-topics/ftp