您需要安装SFTP驱动程序 https://flysystem.thephpleague.com/adapter/sftp/对于 Flysystem,Laravel 使用其文件系统服务库:
composer require league/flysystem-sftp
这是您可以调整的示例配置。添加到disks
数组中配置/文件系统.php:
'sftp' => [
'driver' => 'sftp',
'host' => 'example.com',
'port' => 21,
'username' => 'username',
'password' => 'password',
'privateKey' => 'path/to/or/contents/of/privatekey',
'root' => '/path/to/root',
'timeout' => 10,
]
通过将以下代码添加到新驱动程序来扩展 Laravel 的文件系统boot()
的方法AppServiceProvider
(或其他适当的服务提供商):
use Storage;
use League\Flysystem\Filesystem;
use League\Flysystem\Sftp\SftpAdapter;
...
public function boot()
{
Storage::extend('sftp', function ($app, $config) {
return new Filesystem(new SftpAdapter($config));
});
}
然后你可以像使用本地文件系统一样使用 Laravel 的 API:
Storage::disk('sftp')->put('path/filename.txt', $fileContents);