Moenchfracht's avatar

Storage and retrieving files

Hi, I am using subdomains for my users eg. username.domain.com. Users can upload images to their own storagedisks. in config\filesystems.php I have:

        'user' => [
            'driver' => 'local',
            'root'   => NULL, 
            'visibility' => 'public'
        ],

and my testfile (which should only display the image):

        config(['filesystems.disks.user.root' => storage_path().'/app/'.$username]);
        config(['filesystems.disks.user.url' => 'https://'.$username.'.domain.com/storage']);

The files are stored correctly under storage/username but I can not show the image in my views. I tried with

            <img src="{{ Storage::disk('user')->url('14264228_1080854508651067_2414239900366396390_n.png') }}">

But it doesn't work. Please help. Thanks :)

0 likes
13 replies
36864's avatar

But it doesn't work.

We're going to need a bit more information here. What is the output of the code you've shown? What is the output you were expecting?

36864's avatar

Did you link the storage to be publicly accessible?

36864's avatar

And is the file you uploaded in that folder?

Moenchfracht's avatar

Yes, upload works. The file is stored in the folder storage/app/username

36864's avatar
36864
Best Answer
Level 13

So, when you say yes, what you actually mean, is no.

The file is not in the path you're trying to read it from. You're looking for a file called 14264228_1080854508651067_2414239900366396390_n.png in the storage folder, but your file is in the storage/app/username folder.

Fix your path construction and it should work.

36864's avatar

I just told you what your mistake is. you're trying to load storage/14264228_1080854508651067_2414239900366396390_n.png when your file is instorage/app/username/14264228_1080854508651067_2414239900366396390_n.png

Snapey's avatar

You need to store your file in storage/app/public/username/.... if you want it to appear in storage/username on the outside

Moenchfracht's avatar

Thank you 36864 and Snapey. I changed my symlink to point to storage/app and now it's working :)

36864's avatar

Here, I'll condense all the relevant information into one post:

php artisan storage:linkcreates a symlink from public/storageto storage/app/public as described in the documentation: https://laravel.com/docs/5.6/filesystem#the-public-disk.

That means that, in order for you to display a file as https://example.com/storage/image.png, you need to make sure it's saved to the storage/app/public folder in your server. Likewise, if you store a file in the storage/app/public/{username}/ folder, you can link to it at https://example.com/storage/{username}/image.png.

If you want to use a different structure for your files, you'll have to manage your symlinks manually.

Please or to participate in this conversation.