3

I've created a PHP script in order to make backups of web projects. It consists of make a .tar.gz archive, and the send it to a personal FTP Server (with vsftpd).

From many Web server it works fine, but from one particular Web server the script not works correctly.

Here the part of FTP connection:

$connessione = ftp_ssl_connect($ftpServer, $ftpPort, 300);
if($connessione == false)
{
    echo("connection failed.\n");
    return false;
}
echo("Connection established.\n");
echo("Authenticating for "  .$username . " ...\n");
$resLogin = ftp_login($connessione, $username, $password);
if(!$resLogin)
{
    echo("authentication failed.\n");
    return false;
}
ftp_pasv($connessione, false);

$workingDir = ftp_pwd($connessione);
echo("entering on " . $workingDir . "\n");
echo("Uploading file ... \n");
$fullFilePath = __DIR__ . "/" . $filePath;
$result = ftp_put($connessione, $ftpPath . $file, $fullFilePath, FTP_BINARY);
if($result)
    echo("Upload completed.\n");
else
    echo("Some error occurred.\n");
ftp_close($connessione);

The error returned by ftp_put call is:

PHP Warning: ftp_put(): Failed to establish connection.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Klode
  • 353
  • 3
  • 18

1 Answers1

3

You are explicitly using an FTP active mode. I'm even surprised that works for you on most servers. Do not use the active mode, use a passive mode.

ftp_pasv($connessione, true);

For the active mode to work, your webserver has to have firewall and NAT configured to allow incoming FTP data connections. For details, see my article on the active and passive FTP connection modes.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992