【发布时间】:2011-02-28 11:25:09
【问题描述】:
我正在尝试编写一个将文件上传到 FTP 服务器的小型 php 函数,但我一直收到相同的错误,但我无法通过谷歌搜索问题找到任何解决方法,我希望你们能在这里帮助我...
我得到的错误是:Warning: ftp_put() [function.ftp-put]: Unable to build data connection: No route to host in.
文件是在 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);
?>
【问题讨论】: