Open menu

Learn

SFTP File Listing with PHP: A Comprehensive Guide

Do you learn easier from videos? We also have a video tutorial showing how to list the files from an SFTP server using PHP → Watch video

Transferring files securely between servers is a common task for web developers. One way to achieve this is by using the Secure File Transfer Protocol (SFTP). Listing files on an SFTP server can be a useful task in many scenarios. For example, you may need to display a list of files available on a remote server or synchronize a local directory with a remote directory. With PHP, you can easily retrieve a list of files on an SFTP server and use them in your applications.

Installing phpseclib

To connect to an SFTP server and list files, we will be using the phpseclib package. This is a popular and reliable PHP library for working with various secure network protocols, including SFTP. It provides a pure PHP implementation of multiple protocols, including SFTP. Before proceeding with this tutorial, make sure you have installed the phpseclib package using Composer:
composer require phpseclib/phpseclib:~3.0

Connecting to the SFTP server and listing the files

Now that we have the phpseclib package installed, we can proceed to write the PHP script to connect to the SFTP server and list the files.
require_once('vendor/autoload.php');
use phpseclib3\Net\SFTP;

// The credentials of the SFTP server
$host = 'eu-central-1.sftpcloud.io';
$user = 'my-sftp-user';
$password = 'mQgaT5tT6hf0iqDkgqXtii1tJJXzlTqB';

// Establish the connection
$sftp = new SFTP($host);
$sftp->login($user, $password);

// Loop through the files and show the file name on a new line
foreach ($sftp->nlist() as $key => $fileName) {
  echo $fileName . PHP_EOL;
}

Setting the listing order

You also have to ability to control the order in which files are listed when retrieving a directory listing from an SFTP server. It's possible to sort by name, modification time, access time, and creation time.
// Sort by name asc or desc
$sftp->setListOrder('filename', SORT_ASC);
$sftp->setListOrder('filename', SORT_DESC);

// Sort by size asc or desc
$sftp->setListOrder('size', SORT_ASC);
$sftp->setListOrder('size', SORT_DESC);

// Sort by modification time asc or desc
$sftp->setListOrder('mtime', SORT_ASC);
$sftp->setListOrder('mtime', SORT_DESC);

// Sort by creation time asc or desc
$sftp->setListOrder('ctime', SORT_ASC);
$sftp->setListOrder('ctime', SORT_DESC);

// Sort by access time asc or desc
$sftp->setListOrder('atime', SORT_ASC);
$sftp->setListOrder('atime', SORT_DESC);
But there is more. By combining multiple sort parameters in phpseclib , you can create more advanced and customized sorting orders for the files in your SFTP server. This can be especially useful when working with directories that contain a large number of files, where sorting by just one parameter might not provide enough organization or context for your needs. For example, you could sort files first by size and then by name:
// Sort by size desc and by name asc
$sftp->setListOrder('size', SORT_DESC, 'filename', SORT_ASC);

Recursive listing

One of the parameters that nlist accepts is the $recursive parameter which has false as the default value. This means that if you do not specify a value for the $recursive parameter when calling the nlist method, it will only list the files and directories in the specified directory and will not descend into any subdirectories.
$sftp->nlist(recursive: false); // Same as $sftp->nlist();

// This will output only the directories and files on the first level
Array
(
   [0] => dir1
   [1] => dir2
   [2] => file5.txt
)

// Now let's call it in recursive mode
$sftp->nlist(recursive: true);

// All directories and files in the entire directory tree are included
Array
(
    [0] => dir1
    [1] => dir1/file1.txt
    [2] => dir1/file2.txt
    [3] => dir2
    [4] => dir2/dir3
    [5] => dir2/dir3/file3.txt
    [6] => dir2/file4.txt
    [7] => file5.txt
)