3
votes

I have an secure FTP Server with the login details and from PHP, am trying to connect to that secure FTP Server using ftp_ssl_connect/ftp_connect and ftp_login function and passing all the parameters properly to the function but I am amazed to see that it does not connect.

If I try to connect to that secure FTP Server from command line using ssh than I am able to do so but when I am trying to connect through php code, it does not connect and so I am not sure why this is happening ?

Also what are the other ways to connect to Secure FTP Server using PHP ?

EDIT : I tried using ssh2_sftp but still I was not able to connect to secure FTP Server.

EDIT 2 : Are there any other ways to do SFTP with PHP, please advise.

Update : Added code which used ssh2_sftp to connect to secure FTP Server but it didn't worked and program died out with message Cannot connect to Server

<?php
$connection = ssh2_connect('www.server.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection) or die ("Cannot connect to server");

$stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
?>
3
From your comment to my reply, it seems you already tried ssh2_sftp as well. It may help to edit your question to reflect that, and include the specific problems you're having with that function, i.e. which error message you get when you execute which specific code. Also, including the SSH command that does work may allow validation of your code. - mdb
@mdb : I have updated question with the code that I used to connect, basically I have used it from php.net example for ssh2_sftp. - user260204
Are there any other way to connect to Secure FTP Server using PHP ? - user260204
Clearly there are people all over the place with this same issue - I'm amazed there is not a single solution posted almost anywhere for FTP over SSL - JM4

3 Answers

1
votes

FTP/S (FTP over SSL) is an entirely different protocol than SFTP (FTP over SSH), and you can't use a SSL library to connect to an SFTP server (or vice versa).

I'm no PHP programmer, but it does seem some options to connect to an SSH server are natively available for your platform.

1
votes

Like mdb said, you need to use the ssh2 extension in PHP. If you're using Linux, it's pretty simple as sudo pecl install ssh2. If you're on OS X you need to either do a svn checkout, or follow the instructions here: http://thirdpartycode.com/2010/01/installing-the-php-ssh2-extension-in-snow-leopard-10-6/

1
votes

I've had quite a bit of success with phpseclib, a pure PHP SFTP implementation. Never mind the fact that the PECL extension is hard to install but it's also unreliable and not very portable.