Home Ask Login Register

Developers Planet

Your answer is one click away!

G.W February 2016

Using PHP to set up SSH connection and SFTP

I'm trying to set up a ssh connection, and later a sftp connection to transfer file via FTP to my server. I have used FileZilla to upload files successfully before.

These are my login terminal command and FileZilla login information:

ssh b0xxxxx@linux7.aa.bb.cc.dd
Host: sftp://linux7.aa.bb.cc.dd
Username: b0xx
Port: 22

Here is my php code:

    $ftp_server = "b0xx@linux7.aa.bb.cc.dd";
    $ftp_port = 22; 
    $ftp_user = "b0xx";
    $ftp_pass = "##";
    $connection = ssh2_connect($ftp_server, $ftp_port);
    echo "Successful!\n";
    if (ssh2_auth_password($connection, $ftp_user, $ftp_pass)) {
      echo "Successful\n";
    } else {
      die ('Failed...');
    $sftp = ssh2_sftp($connection);
    ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);


Question 1: Can I just use ftp functions to upload/download file from my server? Or do I must set up ssh?

Question 2: When I execute the code, the website only displays

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

which means that the code does not execute connection successfully. Does anyone know the cause of the bug?

Very much appreciate the help.


AnkiiG February 2016

Seems SSH2 PECL extension is not installed. Install the extension and then try your code.

Kenster February 2016

$ftp_server = "b0xx@linux7.aa.bb.cc.dd";

"b0xx@linux7.aa.bb.cc.dd" isn't a hostname. Hostnames can't have "@" in them. This is probably intended to be a "username@hostname" string, but ssh_connect() doesn't accept a string in this form.

Remove the username portion:

$ftp_server = "linux7.aa.bb.cc.dd";

Post Status

Asked in February 2016
Viewed 3,419 times
Voted 13
Answered 2 times


Leave an answer

Quote of the day: live life