Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tharindulucky's avatar

Access files on a separate server via SFTP in Laravel

I'm trying to access some files on a separate server via SFTP with my Laravel application.

Here's what I have done so far.

I installed league/flysystem-sftp package.

I updated the sftp section in config/filesystems.php file with the credentials I was given.

'sftp' => [
      'driver' => 'sftp',
      'host' => env('BILLS_SFTP_HOST'),
      'username' => env('BILLS_SFTP_USERNAME'),
      'password' => env('BILLS_SFTP_PASSWORD'),
],

Then I created the controller method like this.

private function getBillDetailsSFTP($mobile_number){
    $contents = Storage::disk('sftp')->allFiles('path/to/folder/');
    dd($contents);
}

This doesn't return any errors but returns an empty array .

What's wrong with this implementation ?

0 likes
6 replies
Snapey's avatar

Just comparing to one of my own projects.

I use listContents($path) rather than allFiles.

Its not quite the same because I create a 'driver' first

$driver = Storage::createSFtpDriver([
                'host'     => $system->ftphost,
                'username' => decrypt($system->ftpuser),
                'password' => decrypt($system->ftppass),
                'port'     => '22',
                //'root'     => '',
                //'passive'  => '',
                //'ssl'      => '',
                'timeout'  => '10',
            ]);

$files = collect($driver->listContents($system->ftppath, false));

$system contains client details - each client has their own ftp credentials so I didn't set it up in config.

1 like
tharindulucky's avatar

I tried your code. Still the same result. Doesn't return any error but returns an empty collection.

But, there are files in the directory. Does this have something to do with file permissions ?

Snapey's avatar

Given a certain set of credentials, flysystem should return the same result as say any desktop FTP client. Have you tried it with filezilla / forklift etc

1 like
tharindulucky's avatar

I think I'm making progress here.

I just found out that the files in the root directory is getting listed.

$files = collect($driver->listContents('/'));

This returns all the files and folders in / directory.

But below directory and all other sub directories return empty array.

$files = collect($driver->listContents('/data'));

Any clue ? Thanks!

rtofvnt's avatar

I know this is quite an old question but has anyone had the same problem and got it solved?

vagkaefer's avatar

If you're experiencing this issue, it's probably an incorrect path or incorrect permissions.

The PHP code above is correct and works perfectly.

I suggest you do two things:

  1. Try listing the root:
$allfiles = $files = collect($driver->listContents('/'));

And starting from the root, find the desired path.

  1. Use an FTP/SFTP client (Filezilla-style) to access and make absolutely sure the login details and path are correct.

Note: When accessing SFTP via Filezilla, the client may report something like "/home/username/," which is your initial login path. However, in your PHP file, the path "/home/username" should NOT be entered; it should simply be "/." This causes a lot of confusion.

Check to see if this isn't what's causing your problems.

Please or to participate in this conversation.