如何在 Laravel 5 中使用 ftp?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45368869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:24:00  来源:igfitidea点击:

How to use ftp with laravel 5?

laravellaravel-5ftplaravel-5.4ftp-client

提问by Benubird

The file Storage section of the laravel docs shows an example FTP config, but it doesn't explain how to use it. Can someone give an example of how to make laravel use an ftp connection in addition to a disk connection?

laravel 文档的文件存储部分显示了一个示例 FTP 配置,但没有解释如何使用它。有人可以举例说明如何使laravel 除了磁盘连接之外还使用ftp 连接吗?

回答by Joe

https://laravel.com/docs/5.4/filesystem

https://laravel.com/docs/5.4/filesystem

The beauty of the Laravel filesystem (and a lot of Laravel) is that the commands are "driver agnostic". This means that you define which "driver" (in your case FTP) to use, and configure it, and then all of the comands are the same.

Laravel 文件系统(以及很多 Laravel)的美妙之处在于命令是“驱动程序不可知的”。这意味着您定义要使用的“驱动程序”(在您的情况下为 FTP)并对其进行配置,然后所有命令都相同。

So to upload a file to your ftp filesystem, you would do something like:

因此,要将文件上传到您的 ftp 文件系统,您需要执行以下操作:

Storage::disk('ftp')->put('avatars/1', $fileContents);

Storage::disk('ftp')->put('avatars/1', $fileContents);

The paramenter for the ::disk method above defines which "disk" Laravel should use, so if you had S3 set up, you would do this instead:

上面 ::disk 方法的参数定义了 Laravel 应该使用哪个“磁盘”,所以如果你设置了 S3,你可以这样做:

Storage::disk('s3')->put('avatars/1', $fileContents);

Storage::disk('s3')->put('avatars/1', $fileContents);

回答by Daniel Ortegón

Configure the filesystem.php file in the config folder with the following code:

使用以下代码配置 config 文件夹中的 filesystem.php 文件:

disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

        'ftp' => [
            'driver' => 'ftp',
            'host' => 'ftp.foo.com',
            'username' => 'username',
            'password' => 'password',

            // Optional FTP Settings...
            'port' => 21,
            // 'root' => '',
            // 'passive' => true,
            // 'ssl' => true,
            // 'timeout' => 30,
        ],
    ],

then use:

然后使用:

Storage::disk('ftp')->get('/foo/file');