I need to connect to an SSH FTP server in PHP

Asked

Viewed 1,582 times

1

I have this code:

<?php
$ftp_server="MEUHOST.COM";
$conn_id = ftp_connect($ftp_server, 2222) or die("Couldn't connect to $ftp_server"); 
if (!ftp_connect($ftp_server))
echo "not connected";
else
echo "successful connected";
?>

but it is not connecting in ssh.

  • http://php.net/ftp_ssl_connect

2 answers

1

Well, I believe you’re using the SFTP:

SFTP, meaning SSH File Transfer Protocol, or Secure File Transfer Protocol, is a separate, bundled SSH protocol that works similarly over a secure connection.

To use this feature in PHP, you need ssh2_sftp.

According to the PHP manual:

<?php

$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection);

$stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
?>

A complete example using as a class, which is also on the PHP manual page:

<?php

class SFTPConnection
{
    private $connection;
    private $sftp;

    public function __construct($host, $port=22)
    {
        $this->connection = @ssh2_connect($host, $port);
        if (! $this->connection)
            throw new Exception("Could not connect to $host on port $port.");
    }

    public function login($username, $password)
    {
        if (! @ssh2_auth_password($this->connection, $username, $password))
            throw new Exception("Could not authenticate with username $username " .
                                "and password $password.");

        $this->sftp = @ssh2_sftp($this->connection);
        if (! $this->sftp)
            throw new Exception("Could not initialize SFTP subsystem.");
    }

    public function uploadFile($local_file, $remote_file)
    {
        $sftp = $this->sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');

        if (! $stream)
            throw new Exception("Could not open file: $remote_file");

        $data_to_send = @file_get_contents($local_file);
        if ($data_to_send === false)
            throw new Exception("Could not open local file: $local_file.");

        if (@fwrite($stream, $data_to_send) === false)
            throw new Exception("Could not send data from file: $local_file.");

        @fclose($stream);
    }
}

?>

Use of the above class:

try
{
    $sftp = new SFTPConnection("localhost", 22);
    $sftp->login("username", "password");
    $sftp->uploadFile("/tmp/to_be_sent", "/tmp/to_be_received");
}
catch (Exception $e)
{
    echo $e->getMessage() . "\n";
}

1

First you have to be sure what you want to access SSH or SFTP, are relatively different things, does not work with ftp_connect, use https://secure.php.net/manual/en/function.ssh2-sftp.php if it is SSH only or makes use of additional of https://secure.php.net/manual/en/function.ssh2-sftp.php

If it is SSH

<?php
function ssh_desconectado($reason, $message, $language) {
    printf("O servidor foi desconectado, código [%d] e mensagem: %s\n", $reason, $message);
}

// Ajuste aqui com os dados para conexão
$methods = array(
  'kex' => 'diffie-hellman-group1-sha1',
  'client_to_server' => array(
    'crypt' => '3des-cbc',
    'comp' => 'none'),
  'server_to_client' => array(
    'crypt' => 'aes256-cbc,aes192-cbc,aes128-cbc',
    'comp' => 'none'));

$connection = ssh2_connect('shell.example.com', 22, $methods, array('disconnect' => 'ssh_desconectado'));

if (!$connection) die('conexão falhou');

If it is SFTP

You may have to pass the $methods as in the previous example

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

$sftp = ssh2_sftp($connection);

And the functions you should use to manage SFTP are:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.