morne
morne

Reputation: 4189

Laravel and React Upload image via Storage Facades fail

I want to upload a logo for my reports.

This is a snippet from my uploadLogo function

$file = $request->file;
Storage::disk('logo')->put('logo.png', $file);

I've created a logo profile in filesystems.php like this.

    'logo' => [
        'driver' => 'local',
        'root'   => public_path() . '/img',
        'url' => env('APP_URL').'/public',
        'visibility' => 'public',
    ],

But it eventually created the file in a 'random' ( or misunderstood ) location with a random name.

public\img\logo.png\M4FGLpZzAsyxn8NHiJLxo95EoP7I3CkIWvqkiQsv.png

What am I missing in my setup here?

Upvotes: 0

Views: 19

Answers (1)

Donkarnash
Donkarnash

Reputation: 12845

You can store the file directly of the request's file (UploadedFile) object. And use storeAs to save by the name you supply. The Storage::put and UploadedFile::store` methods generate random names for the filed being stored.

$path = $request->file->storeAs('img', 'logo.png', 'logo');

More info https://laravel.com/docs/8.x/filesystem#storing-files and https://laravel.com/docs/8.x/requests#storing-uploaded-files

Upvotes: 1

Related Questions