问题
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);
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.
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.
So what stumps me is that if all these methods of connecting to the server work, then why won't ftp_connect() function work?
CLARIFICATION:
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.
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.
回答1:
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.
回答2:
A third parameter was added to the function called timeout
. Try something like:
$conn = ftp_connect('site', 22, 360);
回答3:
may be you can try to use "IP" address rather then entire string.
来源:https://stackoverflow.com/questions/21807103/why-does-php-ftp-connect-fail-when-the-server-is-running