0

I'm trying to store a file in the public folder storage/app/public/ but for some reason Laravel just seems to put it in the private storage/app/ folder.

If I understand correctly I'm supposed to just set the visibility to 'public' but that doesn't seem to change anything:

Storage::put($fileName, file_get_contents($file), 'public');

When I call getVisibility I get public so that seems to work fine:

Storage::getVisibility($fileName); // public

These are the settings in my filesystems.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_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

],

1 Answer 1

1

When you call Storage::put, Laravel will use the default disk which is 'local'.

The local disk stores files at its root: storage_path('app'). The visibility has nothing with where the file should be stored.

You need to choose the public disk which will store the files at its root: storage_path('app/public'),

To do that, you need to tell Laravel which disk to use when uploading the file. Basically change your code to this:

Storage::disk('public')->put($fileName, file_get_contents($file), 'public');

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works! But I still don't understand what the 'public' argument in the put method does. I can omit it (or even set it as 'private') and everything still works fine.. I'm confused :P
the visibility argument is useful when you're using amazon s3 as the disk. It will set file's visibility (can be accessed publicly or not. In your case, when using local disk, it has no effect.

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.