简体   繁体   English

为什么服务器运行时PHP ftp_connect失败?

[英]Why does PHP ftp_connect fail when the server is running?

I've been trying to connect via FTP to my server for some time now and can't seem to get it to work. 我一直在尝试通过FTP连接到服务器一段时间,但似乎无法正常工作。

$connection_id = ftp_connect("example.com", 22);

When running this code, it hangs for sometime until PHP eventually tells me the script has been executing for too long and quits. 运行此代码时,它会挂起一段时间,直到PHP最终告诉我该脚本执行了太长时间并退出为止。 It does not fail and return FALSE. 它不会失败并返回FALSE。 It just times out. 它只是超时。

My first instinct was that maybe the server was down, so I pinged it. 我的第一个直觉是服务器可能已关闭,所以我对其进行了ping操作。 The ping was successful and I didn't lose any packets. ping成功,并且我没有丢失任何数据包。 Then I tried using the same parameters to connect with Filezilla and it was able to connect . 然后,我尝试使用相同的参数连接Filezilla并能够连接 I can also SSH to the server. 我也可以SSH到服务器。 I even tried extending the execution time PHP allows for the script but it always hangs up and times out. 我什至尝试延长PHP允许脚本执行的时间,但是它总是挂断并超时。

So what stumps me is that if all these methods of connecting to the server work, then why won't ftp_connect() function work? 那么让我感到困惑的是,如果所有这些连接到服务器的方法都起作用,那么ftp_connect()函数为什么不起作用?

CLARIFICATION: 澄清:

Some of you have wondered why am I using port 22 (the SSH port) for connecting. 你们中有些人想知道我为什么要使用端口22(SSH端口)进行连接。 I tried the default port for FTP (Port 21) at first, but the ftp_connect() function immediately returned FALSE, which according to the PHP manual means there was some error. 我最初尝试使用FTP的默认端口(端口21),但是ftp_connect()函数立即返回FALSE,根据PHP手册,这意味着出现了一些错误。

I was also unable to connect through Port 21 with FileZilla. 我也无法通过端口21与FileZilla连接。 So I decided to use the SFTP Port instead, which is Port 22. Doing this connected successfully in FileZilla. 因此,我决定改用SFTP端口,即端口22。在FileZilla中成功连接。

A third parameter was added to the function called timeout . 第三个参数已添加到名为timeout的函数中。 Try something like: 尝试类似:

$conn = ftp_connect('site', 22, 360);

ftp_connect is not meant to be used for connecting to SFTP servers. ftp_connect不能用于连接到SFTP服务器。 Check out ssh2_sftp , which uses a different methodology then standard FTP. ssh2_sftpssh2_sftp ,它使用与标准FTP不同的方法。

可能是您可以尝试使用“ IP”地址而不是整个字符串。

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

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