问题描述
我有一台PC,该PC通过php运行一些FTP,我知道它在1-2个月前就可以正常工作,但是现在回到它上面,我发现该PC不再工作了.我知道我一直在使用PC,但是我想不到可能发生了什么变化.
I have a PC that is running some FTP via PHP that I know used to work 1-2 months ago, but now I return to it I find that the PC is no longer working. I know I have been using the PC but I cannot think of what might have changed.
PHP抛出读取错误消息
The PHP is throwing out error messages reading
...当我使用ftp_put()
函数时.
...when I use the ftp_put()
function.
我使用的缩减代码是:
<?php
$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
$server="***.***.***.***";
$port=21;
echo "<LI>Connecting to $server:$port<BR>";
$conn_id = ftp_connect($server,$port,9999999) or die("<BR>Unable to connect to ".$server.":$port server.");
if ( !$conn_id ) {
$errmsg = $php_errormsg;
echo "<BR><LI>ERR:$errmsg";
}
else {
$passive=false;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
$user="*********";
$pass="*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
$msg = "Failed to login to $selected_server as $user; <BR>check logincredentials in the Settings";
echo "<BR><LI>$msg";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
return $msg;
}
ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 10000);
if (!@ftp_put($conn_id, "test.txt", "C:......test.txt", FTP_BINARY)) {
echo "<BR><LI>ftp_put failed";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
}
echo "<HR>Done";
}
?>
将其作为网页运行时的输出是
the output when running this as a webpage is
Connecting to ***.***.***.***:21
Setting Passive Mode=
Connecting as *******/*****
ftp_put failed
ERR:ftp_put(): Unable to build data connection: Connection refused
Done
结果是ftp_put()
给出错误消息,并在服务器上留下一个具有正确文件名的零(0)字节文件.
The result is that the ftp_put()
gives the error message and leaves a zero (0) byte file with the right filename on the server.
奇怪的是,
- 相同的代码/连接信息可以在其他笔记本电脑上正常使用
- 推送文件时,使用FileZilla可以使相同的连接信息正常工作
- 问题在多台服务器上发生(即,不仅仅是一个特定的目的地出现了问题)
此外,这似乎与被动模式无关(启用和未启用此功能都会失败)
Also, this doesn't seem to have anything to do with the passive mode (it fails with and without this enabled)
有人有什么建议吗?
谢谢
安倍(Abe)
Does anyone have any suggestions?
Thanks
Abe
推荐答案
您正在使用活动的FTP模式.在活动模式下,服务器尝试连接到客户端.在大多数网络配置中,这是不可能的,因为客户端计算机通常位于防火墙后面.
You are using the active FTP mode. In the active mode the server tries to connect to the client. In most network configurations, that's not possible as the client machine is usually behind a firewall.
这就是服务器失败的原因:
That's why the server fails with:
在这种情况下,特别是ProFTPD错误消息.
It's specifically ProFTPD error message for this situation.
有关详细信息,请参见我在主动和被动FTP连接模式上的文章.
See my article on the active and passive FTP connection modes for details.
该代码可以在其他计算机上运行,如果它们已禁用防火墙或具有允许在非特权端口上传入流量的规则.
The code can work on other machines, if they have firewall disabled or if they have rules that allow incoming traffic on unprivileged ports.
FileZilla之所以起作用,是因为它默认为被动模式(就像大多数现代FTP客户端一样).
FileZilla works because it defaults to the passive mode (as most modern FTP clients do).
您已经声称也尝试过被动模式,但仍收到相同的错误消息.
You have claimed to try the passive mode too, yet to get the same error message.
那是因为您错误地使用了ftp_pasv
呼叫.
That's because you are using the ftp_pasv
call incorrectly.
您必须将ftp_pasv
调用移到 之后,然后移到ftp_login
.
You have to move the ftp_pasv
call after the ftp_login
.
$user = "*********";
$pass = "*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
// ...
}
$passive = true;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
文档明确建议:
对于类似的问题(仅适用于Pure-FTPd),请参见通过FTP上传PHP-ftp_put()我不会打开连接到xxxx(仅连接到yyyy).
这篇关于PHP ftp_put返回“无法建立数据连接:连接被拒绝";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!