Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I configure the league/flysystem-sftp package in Laravel
    Laravel SFTP flysystem configuration package setup environment variables storage


How do I configure the league/flysystem-sftp package in Laravel


Here are the steps to configure the `league/flysystem-sftp` package in Laravel:

1. Install the package:


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

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. 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.

Additionally, you may need to adjust the configuration options based on your specific SFTP server requirements, such as the port, root directory, or timeout settings.

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