简体   繁体   English

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

[英]Upload text file to Google uploads via PHP FTP PUT

I am trying to upload a text file that is created from a database via PHP. 我正在尝试上传通过PHP从数据库创建的文本文件。

The text file is created OK, but when I try and upload the file via PHP FTP Put it fails. 文本文件创建成功,但是当我尝试通过PHP FTP上传文件时,它失败了。

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. 警告:ftp_put():端口IP与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. 在第100行的/home/sites/mysite.co.uk/public_html/admin/controllers/generate_feed.php中错误:无法将products_admin.txt上传到uploads.google.com。

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

When I used FTP with my Google Compute Engine server, I had the same problem which occured because server is behind Google's firewall. 当我在我的Google Compute Engine服务器上使用FTP时,由于服务器位于Google的防火墙后面,因此出现了同样的问题。 You can read more about this problem here . 您可以在此处阅读有关此问题的更多信息。 You can try enabling passive mode which doesn't care about IP with function ftp_pasv . 您可以尝试使用功能ftp_pasv启用不关心IP的被动模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM