上传文本文件到 Google 通过 PHP FTP PUT 上传

Upload text file to Google uploads via PHP FTP PUT

我正在尝试上传通过 PHP 从数据库创建的文本文件。

文本文件创建成功,但是当我尝试通过 PHP 上传文件时 FTP 上传失败。

我的代码:

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

我收到的错误信息是

警告:ftp_put():端口 IP 与 176.32.230.48 不同。在 /home/sites/mysite.co.uk/public_html/admin/controllers/generate_feed.php 的第 100 行错误:无法将 products_admin.txt 上传到 uploads.google.com.

您可能只需要激活被动模式:

...
$login_result = ftp_login($ftp_connect, "{usernamehere}", "{passwordhere}") or die("ERROR: Username or Password incorrect.");
ftp_pasv($ftp_connect, true);
...

当我将 FTP 与我的 Google Compute Engine 服务器一起使用时,我遇到了同样的问题,因为服务器在 Google 的防火墙后面。您可以阅读有关此问题的更多信息 . You can try enabling passive mode which doesn't care about IP with function ftp_pasv