8

I am trying to upload a text file that is created from a database via PHP.

The text file is created OK, but when I try and upload the file via PHP FTP Put it fails.

My code:

$filename = "products_admin.txt";
$handle = fopen($filename, 'w+');
fwrite($handle, $content);
fclose($handle);
echo "Attempting to connect to <i>uploads.google.com</i>...<br />";
$ftp_connect = ftp_connect("uploads.google.com", "21", "5000") or die("failed to connect.");
$login_result = ftp_login($ftp_connect, "{usernamehere}", "{passwordhere}") or die("ERROR: Username or Password incorrect.");

if((!$ftp_connect) || (!$login_result)) {
    echo "ERROR: Couldn't connect to <i>uploads.google.com</i>, upload failed.<br /><br />";
    echo "<a href=\"javascript:location.reload(true)\">Try Again</a>";
    exit;
} else {
    echo "Connected to <i>uploads.google.com</i>...<br />";
    $upload = ftp_put($ftp_connect, $filename, $filename, FTP_ASCII);
    if(!$upload) {
        echo "ERROR: Failed to upload ".$filename." to <i>uploads.google.com</i>.<br /><br />";
        echo "<a href=\"javascript:location.reload(true)\">Try Again</a>";
    } else {
        echo "Uploading <i>".$filename."</i> to <i>Froogle</i>...<br />";
        echo "Successfully uploaded <i>".$filename."</i> to <i>uploads.google.com</i>.<br /><br />";
        echo "Done.";
    }
}
ftp_close($ftp_connect);

The error message I get is

Warning: ftp_put(): PORT IP is not same as 176.32.230.48. in /home/sites/mysite.co.uk/public_html/admin/controllers/generate_feed.php on line 100 ERROR: Failed to upload products_admin.txt to uploads.google.com.

Pete Naylor
  • 776
  • 2
  • 14
  • 33

2 Answers2

4

You probably just need to activate passive mode:

...
$login_result = ftp_login($ftp_connect, "{usernamehere}", "{passwordhere}") or die("ERROR: Username or Password incorrect.");
ftp_pasv($ftp_connect, true);
...
Oli
  • 1,132
  • 1
  • 12
  • 26
  • 1
    One note from the comments section in the [docs](http://php.net/manual/en/function.ftp-pasv.php): _If you are still having problems after choosing ftp_pasv() you should refresh the connection through ftp_close() / ftp_connect() after x files and/or y GB have been transfered._ – CarlosCarucce Aug 16 '16 at 14:40
0

When I used FTP with my Google Compute Engine server, I had the same problem which occured because server is behind Google's firewall. You can read more about this problem here. You can try enabling passive mode which doesn't care about IP with function ftp_pasv.

Community
  • 1
  • 1
Jehy
  • 4,729
  • 1
  • 38
  • 55