celsomtrindade
celsomtrindade

Reputation: 4671

PHP ftp_put create file but doesn't move

I'm trying to send a file via FTP connection from one server to another. I'm able to create the connection and send the file to the folder I want.

The file is created, but it has 0bytes. Also, the php function fails after some time with this error message:

Connection timed out

I also changed the permissions on the folder on the server receiving the file, it has public read, write and execute permissions, however, when the new file arrives on the server, it doesn't have the default permission, but only read.

What I might be doing wrong?

This is my code:

$localFile = $_SERVER['DOCUMENT_ROOT'].'/images/'.$file;
$moveFile  = '/public_html/images/'.$file;

$ftp_server   = 'ftp.mywebsite.com';
$ftp_username = 'xxxxxx';
$ftp_password = 'xxxxxx';

$ftp_connection = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_login      = ftp_login($ftp_connection, $ftp_username, $ftp_password);

if ( ftp_put($ftp_connection, $moveFile, $localFile, FTP_ASCII) ) {
    print_r('Completed');
} else {
    print_r('Error');
}

Upvotes: 1

Views: 913

Answers (1)

Veshraj Joshi
Veshraj Joshi

Reputation: 3589

I had same kind of issue and solved by setting ftp_pasv($conn_id, true); just after ftp login- my code is like below-

$ssl_connection_id = ftp_ssl_connect(FTP_HOST);
if($ssl_connection_id) 
{
  $login = ftp_login($ssl_connection_id,FTP_USERNAME, FTP_PASSWORD);
  ftp_pasv($ssl_connection_id, true);
  if($login)
  {
     if (ftp_put($ssl_connection_id,FTP_PATH.$filename, $localFile, FTP_ASCII)) 
     {
         echo "successfully uploaded \n";

     } 
   }
}

thats all, another difference is that, i use make use of IP address instead of url(ftp.mywebsite.com) like 5.xx.xx.xxx

Upvotes: 1

Related Questions