Home » Php » Upload text file to Google uploads via PHP FTP PUT

Upload text file to Google uploads via PHP FTP PUT

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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);
...

Answer:

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.