How to use ftp with laravel 5?

13,940

Solution 1

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.

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

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:

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

Solution 2

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

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');
Share:
13,940
Benubird
Author by

Benubird

Updated on July 14, 2022

Comments

  • Benubird
    Benubird almost 2 years

    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?