0

I have created a custom disk such as

'custom-ftp' => [
            'driver' => 'ftp',
            'host' => 'imn.tra.de',
            'username' => '',
            'password' => '',
            'port'     => 21,
            'ssl'      => true,
        ],

but i want the username and password to be set by each user in my database and call the disk accordingly,

rightnow what i am doing is before uploading the file to ftp, im doing

 $user = Auth::user();
    $setting = $user->setting;

    Config::set('filesystems.disk.custom-ftp.username', $setting->user_name);
    Config::set('filesystems.disk.custom-ftp.password', $setting->password);

And later

  Storage::disk('custom-ftp')->put($file, $localFile);

But i am getting Like this : Could not login with connection: imn.tra.de::21, username: as this is still picking the username from config where it is empty

10
  • You can add settings field to users table and set them from your dashboard or you can use some settings package. Where is some tutorial for you laracasts.com/lessons/managing-mass-user-settings Commented Aug 3, 2021 at 6:27
  • I am already doing saving the settings in database and updating the config file before uploading the file to ftp, kindly review my edited question Commented Aug 3, 2021 at 6:34
  • What is the problem with your aproach? It's not working? Commented Aug 3, 2021 at 6:42
  • 1
    Config files are used for static settings. Dynamic settings shouldn't be put in config this way Commented Aug 3, 2021 at 6:48
  • 1
    You may set configuration variables at runtime by passing an array of key / value pairs. However, note that this function only affects the configuration value for the current request and does not update your actual configuration values: config(['app.debug' => true]); (laravel.com/docs/master/helpers#method-config) Commented Aug 3, 2021 at 6:48

1 Answer 1

2

Config files are used for static settings and are meant only for settings specific to application not to user.

Dynamic settings shouldn't be put in config.

You should create FTP driver on the fly:

$ftp = Storage::createFtpDriver([
    'driver' => 'ftp',
    'host' => 'imn.tra.de',
    'username' => $setting->user_name,
    'password' => $setting->password,
    'port'     => 21,
    'ssl'      => true,
]);

$ftp->put($file, $localFile);

Reusable solution would look like:

<?php

namespace App\Services;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class UserFtpStorageService
{
    public function __construct()
    {
        $userSettings = Auth::user()->settings;
        $this->ftp = Storage::createFtpDriver([
            'driver' => 'ftp',
            'host' => 'imn.tra.de',
            'username' => $userSettings->user_name,
            'password' => $userSettings->password,
            'port'     => 21,
            'ssl'      => true,
        ]);
    }

    public function put(string $path, $contents, $options = []) : bool
    {
        return $this->ftp->put($path, $contents, $options);
    }
}

And then you use it like:

(new UserFtpStorageService())->put($path, $contents);
Sign up to request clarification or add additional context in comments.

3 Comments

How this can be used in future? like lets say we use this for uploading file one time, then next time do we need to create ftpdriver again?
You can just create a class that abstracts it all an do something like: UserFtpStorage::put($file, $localFile)
@user8076689 I added example to my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.