Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I resolve the "Class 'LeagueFlysystemSftpSftpAdapter' not found" error in Laravel
    Laravel SFTP Flysystem SftpAdapter error fix PHP configuration storage


How do I resolve the "Class 'League\Flysystem\Sftp\SftpAdapter' not found" error in Laravel


To resolve the "Class 'League\Flysystem\Sftp\SftpAdapter' not found" error when using the SFTP driver in Laravel, follow these steps:

1. Install the required package:


composer require league/flysystem-sftp-v3 "^3.0"

This will install the `league/flysystem-sftp-v3` package, which provides the `SftpAdapter` class.

2. Configure the SFTP disk in your `config/filesystems.php` file:

php
'disks' => [
    // ...
    'sftp' => [
        'driver' => 'sftp',
        'host' => env('SFTP_HOST'),
        'username' => env('SFTP_USERNAME'),
        'password' => env('SFTP_PASSWORD'),
        // Settings for SSH key based authentication with encryption password
        'privateKey' => env('SFTP_PRIVATE_KEY'),
        'passphrase' => env('SFTP_PASSPHRASE'),
        // Optional SFTP settings
        'port' => env('SFTP_PORT', 22),
        'root' => env('SFTP_ROOT', ''),
        'timeout' => 30,
    ],
    // ...
]

3. Add the necessary environment variables to your `.env` file:


SFTP_HOST=your-sftp-host.com
SFTP_USERNAME=your-sftp-username
SFTP_PASSWORD=your-sftp-password
SFTP_PRIVATE_KEY=/path/to/your/private/key
SFTP_PASSPHRASE=your-private-key-passphrase
SFTP_PORT=22
SFTP_ROOT=/path/to/sftp/root

4. Clear the config cache:


php artisan config:clear

5. Use the SFTP disk in your application:

php
// List all files in the SFTP root directory
$files = Storage::disk('sftp')->allFiles('/');

// Get the contents of a file
$contents = Storage::disk('sftp')->get('path/to/file.txt');

// Write a file to the SFTP disk
Storage::disk('sftp')->put('path/to/file.txt', 'File contents');

// Delete a file from the SFTP disk
Storage::disk('sftp')->delete('path/to/file.txt');

Make sure to replace the placeholders in the configuration with your actual SFTP server details and environment variable values.

By following these steps, you should be able to resolve the "Class 'League\Flysystem\Sftp\SftpAdapter' not found" error and successfully use the SFTP driver in your Laravel application.

Citations:
[1] https://github.com/barryvdh/laravel-elfinder/issues/297
[2] https://laravel.com/docs/11.x/filesystem
[3] https://sftpcloud.io/learn/php/how-to-use-sftp-as-a-laravel-filesystem
[4] https://www.tutsplanet.com/laravel-download-files-to-storage-from-sftp/
[5] https://laracasts.com/discuss/channels/general-discussion/using-leagues-sftp-adapter-with-flysystem