Secure FTP Connection using PHP

2019-07-03 10:32发布

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');
?>

标签: php ftp sftp
3条回答
趁早两清
2楼-- · 2019-07-03 11:15

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-07-03 11:23

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.

查看更多
Deceive 欺骗
4楼-- · 2019-07-03 11:25

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/

查看更多
登录 后发表回答