Connecting to SFTP with PHP

If you need to connect to SFTP using PHP then the simplest approach I’ve found is to use phpseclib, a library of functions for secure communications.

The library is a Composer package so you will need to have Composer installed, then just require the package as usual:-

$ composer require phpseclib/phpseclib

The library provides what it refers to as a “pure-PHP implementation of SFTP” and supports the most widely used versions of SFTP.

To initiate a connection a new object of SFTP is created with the address of the remote server passed to the constructor. To authenticate our SFTP connection we can pass the login credentials using SFTP::login():-

use phpseclib\Net\SFTP;

$sftp = new SFTP('www.example.com');

if (!$sftp->login('username', 'password')) {
	throw new Exception('Login failed');
}

If we’ve got a private key we need to use the RSA class so that we can pass the key in place of the password to the SFTP::login() method:-

use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP;

$sftp = new SFTP('www.example.com');

$Key = new RSA();
// If the private key has a passphrase we set that first
$Key->setPassword('passphrase');
// Next load the private key using file_gets_contents to retrieve the key
$Key->loadKey(file_get_contents('path_to_private_key'));

if (!$sftp->login('username', $Key)) {
	throw new Exception('Login failed');
}

The SFTP class provides methods for SFTP similar to those provided by PHP’s FTP extension. For example, to get a list of files for the current directory we use nlist() (equivalent of ftp_nlist):-

$files = $sftp->nlist();

To change directory use chdir():-

$sftp->chdir('directory_name');

To get a file from the remote server use get():-

$sftp->get('remote_file', 'local_file');

This saves the remote_file to local_file. If the second parameter is left empty then the method returns the contents of the remote file if successful (otherwise it returns false).

To upload a file to the remote server use put():-

$sftp->put('remote_file', 'contents for remote file');

There are many other methods available and can be found in the SFTP class. The class is well documented with thorough comments. The above methods should get you started though.

Hopefully you can see that phpseclib provides a simple way of working with a SFTP connection.

Related Content

Published on


Comments

  1. Lusine |

    Thanks for the article!

  2. thomas |

    or ftp_ssl_connect

  3. thukten |

    hello
    I tired your way but it throws and error
    Fatal error: Class ‘vendor\phpseclib\phpseclib\phpseclib\Net\SFTP’ not found in C:\xampp\htdocs\PHPSEC\test.php on line 4

    Would be grateful if you can render me some help

  4. Klemens |

    @thukten
    late but I hope not to late ;-) you have to write:
    <?php
    require “vendor/autoload.php”;

    use phpseclib\Crypt\RSA;
    use phpseclib\Net\SFTP;

    $sftp = new SFTP;
    .
    .
    .

  5. MAP |

    Dear Andy

    I have tried to use your code but i am getting below error

    <b>Fatal error</b>: Class ‘phpseclib\Net\SFTP’ not found in <b>C:\xampp\htdocs\simple\backend\home_backend.php</b> on line <b>121</b><br />

    here is my code
    use \phpseclib\Net\SFTP;

    $sftp = new \phpseclib\Net\SFTP;

    i am getting error in the above line.

    can you help me here ?

  6. prasanjit mishra |

    how will i send a file from local to remote location?

Leave a Comment
  • You will need to preview your comment before you can submit it.