2016-08-08 178 views
8

我想上传一个文本文件,通过PHP从数据库中创建。上传文本文件到谷歌上传通过PHP FTP PUT

文本文件已创建好,但是当我尝试通过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。第100行中的/home/sites/mysite.co.uk/public_html/admin/controllers/generate_feed.php错误:无法将products_admin.txt上传到uploads.google.com。

回答

4

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

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

来自[docs](http://php.net/manual/en/function.ftp-pasv.php)注释部分的注释:如果选择ftp_pasv()后仍然有问题,则应刷新在x文件和/或y GB之后通过ftp_close()/ ftp_connect()进行连接._ – CarlosCarucce

0

当我使用FTP和我的谷歌Compute Engine的服务器,我有这发生,因为服务器是落后于谷歌的防火墙同样的问题。你可以阅读更多关于这个问题here。您可以尝试使用功能ftp_pasv不关心IP的被动模式。