服务器运行时,为什么PHP ftp_connect失败? [英] Why does PHP ftp_connect fail when the server is running?

查看:121
本文介绍了服务器运行时,为什么PHP ftp_connect失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试通过FTP连接到服务器,但似乎无法正常工作.

I've been trying to connect via FTP to my server for some time now and can't seem to get it to work.

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

运行此代码时,它会挂起一段时间,直到PHP最终告诉我脚本执行时间过长并退出为止.它不会失败并返回FALSE.只是超时.

When running this code, it hangs for sometime until PHP eventually tells me the script has been executing for too long and quits. It does not fail and return FALSE. It just times out.

我的第一个直觉是服务器可能已关闭,因此我对其进行了ping操作.ping成功,并且我没有丢失任何数据包.然后,我尝试使用相同的参数连接Filezilla并能够连接.我也可以SSH到服务器.我什至尝试延长PHP允许脚本执行的时间,但是它总是挂断并超时.

My first instinct was that maybe the server was down, so I pinged it. The ping was successful and I didn't lose any packets. Then I tried using the same parameters to connect with Filezilla and it was able to connect. I can also SSH to the server. I even tried extending the execution time PHP allows for the script but it always hangs up and times out.

让我感到难过的是,如果所有这些连接到服务器的方法都起作用,那么ftp_connect()函数为什么不起作用?

So what stumps me is that if all these methods of connecting to the server work, then why won't ftp_connect() function work?

澄清:

有些人想知道我为什么要使用端口22(SSH端口)进行连接.首先,我尝试使用FTP的默认端口(端口21),但是ftp_connect()函数立即返回FALSE,根据PHP手册,这意味着出现了一些错误.

Some of you have wondered why am I using port 22 (the SSH port) for connecting. 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.

我还无法通过端口21与FileZilla连接.因此,我决定改用SFTP端口,即端口22.在FileZilla中成功连接.

I was also unable to connect through Port 21 with FileZilla. So I decided to use the SFTP Port instead, which is Port 22. Doing this connected successfully in FileZilla.

推荐答案

ftp_connect 并不用于连接SFTP服务器.查看 ssh2_sftp ,它使用不同于标准FTP的方法.

ftp_connect is not meant to be used for connecting to SFTP servers. Check out ssh2_sftp, which uses a different methodology then standard FTP.

这篇关于服务器运行时,为什么PHP ftp_connect失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆