Alex
Alex

Reputation: 45

Upload photo in Laravel

Laravel file upload gives the user error when trying to upload images

Get error: local.ERROR: Driver [] is not supported.

How to fix its problem?

public function channelAvatar(Request $request, Channel $channel)
{
    // validate
    $this->validate($request, [
        'photo' => ['required', 'image', Rule::dimensions()->minWidth(250)->minHeight(250)->ratio(1 / 1)],
    ]);

    // fill variables
    $filename = time() . str_random(16) . '.png';
    $image = Image::make($request->file('photo')->getRealPath());
    $folder = 'channels/avatars';

    // crop it
    $image = $image->resize(250, 250);

    // optimize it
    $image->encode('png', 60);

    // upload it
    Storage::put($folder.'/'.$filename, $image->__toString());
    $imageAddress = $this->webAddress() . $folder . '/' . $filename;

    // delete the old avatar
    Storage::delete('channels/avatars/' . str_after($channel->avatar, 'channels/avatars/'));

    // update channel's avatar
    $channel->update([
        'avatar' => $imageAddress,
    ]);
    $this->putChannelInTheCache($channel);

    return $imageAddress;
}

Uploading locally or to FTP still gives the same error.

Upvotes: 1

Views: 198

Answers (1)

Mihir Bhende
Mihir Bhende

Reputation: 9045

Whenever you use a Storage option without a specific disk, Laravel uses the default driver. It seems like you have specified local as default driver but you do not have it configured.

As per your config/filesystem.php you have :

'ftp' => [ 
    'driver' => 'ftp', 
    'host' => env('FTP_HOST', 'test.something.net'), 
    'username' => env('FTP_USERNAME', 'someusername'), 
    'password' => env('FTP_PASSWORD', '*******'), 
],

So you need to specify this as a default driver. You can do that by adding :

FILESYSTEM_DRIVER=ftp inside the .env file.

And then inside the 'config/filesystem.php` add following :

'default' => env('FILESYSTEM_DRIVER', 'local'),

Now whenever you do Storage::something() it will use default driver. (Something you will have local as the default one`

You can also specify it if you would like :

Storage::disk('ftp')->something() But if your all storage operations use one disk then better specify as default.

Upvotes: 1

Related Questions