Skip to content

Instantly share code, notes, and snippets.

@rtconner
Last active June 27, 2024 09:07
Show Gist options
  • Save rtconner/40b415073378da28b2bf to your computer and use it in GitHub Desktop.
Save rtconner/40b415073378da28b2bf to your computer and use it in GitHub Desktop.
Provider so you can add a 'sftp' connection in Laravel 5 filesystems.php - "Call to undefined method League\Flysystem\Filesystem::createSftpDriver"
<?php
namespace App\Providers;
use League\Flysystem\Sftp\SftpAdapter;
use Storage;
use League\Flysystem\Filesystem;
use Illuminate\Support\ServiceProvider;
/**
* Class SftpServiceProvider
* @package App\Providers
*
* composer require league/flysystem-sftp "~1.0"
*
* Add this file to the Providers directory and update your app.php config
*
* Sample config to put in filesystems.php config:
* 'sftp' => [
* 'driver' => 'sftp',
* 'host' => 'example.com',
* 'port' => 22,
* 'username' => 'username',
* 'password' => 'password',
* 'privateKey' => 'path/to/or/contents/of/privatekey',
* // 'root' => '/path/to/root',
* // 'timeout' => 10,
* // 'directoryPerm' => 0755
* ],
*/
class SftpServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Storage::extend('sftp', function($app, $config) {
unset($config['driver']);
foreach($config as $key => $value) {
if(!strlen($value)) {
unset($config[$key]);
}
}
$adapter = new SftpAdapter($config);
return new Filesystem($adapter);
});
}
/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
//
}
}
@karnickoala
Copy link

I've found useful to my case your sftp driver extension to filesystem configs. Anyway after creating your SftpServiceProvider and adding the reference in provider array in config/app.php , I keep have returning back an error with SftpServiceProvider not found. I already tried : composer dump-autoload. No success until now.

@martonbaranyai
Copy link

Worked perfect for me, thanks! This helped to make the backups work with these packages:

    "graham-campbell/flysystem": "^3.3",
    "league/flysystem-sftp": "^1.0",
    "spatie/laravel-backup": "^2.8"

@defaye
Copy link

defaye commented Jun 9, 2017

You might be able to get away with doing $config = array_filter($config); would you not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment