我正在尝试编写一个小的PHP函数,将文件上传到FTP服务器,我一直得到相同的错误,但我找不到谷歌搜索问题的任何修复,我希望你们可以帮助我在这里...
我得到的错误是:警告:ftp_put()[function.ftp-put]:无法构建数据连接:无主机路由。
该文件是在FTP服务器上创建的,但它是零字节。
以下是代码:
<?php
$file = "test.dat";
$ftp_server="ftp.server.com";
$ftp_user = "myname";
$ftp_pass = "mypass";
$destination_file = "test.dat";
$cid=ftp_connect($ftp_server);
if(!$cid) {
exit("Could not connect to server: $ftp_server\n");
}
$login_result = ftp_login($cid, $ftp_user, $ftp_pass);
if (!$login_result) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}
$upload = ftp_put($cid, $destination_file, $file, FTP_BINARY);
if (!$upload) {
echo "Failed upload for $source_file to $ftp_server as $destination_file<br>";
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($cid);
?>
答案 0 :(得分:8)
我忘了使用以下方式将FTP置于被动模式:
ftp_pasv($cid, true);