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

时间:2016-08-08 10:07:55

标签: php upload ftp port google-shopping-api

我正在尝试上传通过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():PORT IP与176.32.230.48不同。在/home/sites/mysite.co.uk/public_html/admin/controllers/generate_feed.php第100行错误:无法将products_admin.txt上传到uploads.google.com。

1 个答案:

答案 0 :(得分:4)

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

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