SFTP is a standard and secure protocol through which parties can safely transfer and share data and files. In any case, engaging with an SFTP server programatically can be challenging. In the following post, we’ll build a full fledged PHP based SFTP client, step by step, so that by the end of the guide, you’ll be able to do it all on your own!

Requirements

PHP doesn’t come bundled with the SFTP libraries from the get-go, so we’ll need to install the required library ssh2 using PECL. The library relies on libssh2, so you must ensure that it is first installed on your machine. For example, on Ubuntu machines, run the following:

$ apt-get install libssh2-1-dev
$ pecl install -a ssh2-1.3

In accordance with the 12 factor app cloud development methodology, you should explicitly define your app’s dependencies, which would make our composer.json file look like this:

{
  "require": {
    "ext-ssh2": "*"
  }
}

Now the real fun begins!

Creating and dropping SFTP connections

We’ll begin by creating a new class that encapsulates the functionality required for our SFTP client. The public methods we first add allow us to connect to the server using either password authentication or key authentication, and provide the means to disconnect an existing session.

To put our brand new methods to use, we’ll add a main function that instantiates our class and calls the auth_password function to start a connection, as well as close the connection with the disconnect function immediately after. We’ll use the environment variable SFTPTOGO_URL to obtain all the required information for connecting to an SFTP server in a URI format: sftp://user:password@host. Within our main function, the variable is parsed to extract the username, password, host and optional port.

Listing Files

Now that we have set up a successful connection, we can add a function to our class and use it to list files on the remote SFTP server. The listFiles function takes a $remote_dir argument and returns an array of file names, along with their size and modification timestamp. To call the function, simply pass a path (you can start with . to traverse the current directory content) and then process the array you get.

Upload File

Now it’s time to upload a file. Use the uploadFile function and pass two arguments: the path to the local file to upload and the target remote path. A function call would look like this: $client->uploadFile("./local.txt", "./remote.txt");

Download File

Last but not least: Use the downloadFile function to download a file. Pass the path to the remote file and a local path in which the downloaded file will be stored, to the function. You would call the function like this: $client->downloadFile("./remote.txt", "./download.txt");

SFTP To Go with maximum security and reliability
SFTP To Go offers managed SFTP/FTPS/S3 as a service - maximum reliability, security, availability, with 1 minute setup. Great for companies of any size, any scale.
Check out SFTP To Go!

The Whole Thing

That’s it! If you would like to run the entire program from start to finish, copy the following code and save it as main.php:

To run the code, use the command:

php main.php

You can also clone our repo or check out more code samples on Github.