Laravel Storage symlink
Asked Answered
V

3

11

I'm trying to access files that are stored in storage/app/.../.../file.png.

I have created a symlink, linking my storage folder to public/storage.

When I try to access the appropriate file, my server throws a 404, saying file not found. Clearly I'm not accessing it the right way but I dont know what to change.

<img src="{{asset('storage/app/puppy/18/ax2Fk08nyWrV6TwTOnsXNCkNGuIdFebB7TTfPYGb.png')}}" alt="">

enter image description here

Vassily answered 3/8, 2018 at 15:40 Comment(1)
Did you do php artisan storage:link?Catena
G
37

when you create symlink in laravel then it create symlink of storage/app/public folder to public/storage folder. That means if you want to access any files publicly then place all your files inside storage/app/public folder and access it like this

<img src="{{asset('storage/puppy/18/test.png')}}" alt="">

Here it means the file test.png should be physically at storage/app/public/puppy/18 folder.

Execute php artisan storage:link for creating symlink in laravel

Hope it clear you

Generable answered 3/8, 2018 at 15:52 Comment(3)
You're amazing. You seem to be the one answering all of my questions the clearest.Vassily
ah, so glad to help you. happy codingGenerable
I have a laravel 6 app. Everything seems to be in place. But css and js are not loading. When I do ctrl + u I get this i.postimg.cc/Fs58WDJQ/Untitled.jpg and when I Inspect and go to network tab, I see 404 status. I do not know why. This is the github repo I am trying to use github.com/cmate5614530/inventory-managementSalsify
H
3

For shared hostings try below solutions

Code:

1) Remove storage folder in public_html/public/storage

2) Create a php file for example symlink.php in public_html folder

3) Add codes to symlink.php file

$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink process successfully completed';

4) Go to web site example.com/symlink.php

That is all...

Video url from youtube => https://www.youtube.com/watch?v=svyN8-PjGHc

Heterophony answered 5/6, 2020 at 10:58 Comment(6)
I can't reach the file, it gives 404 LOLBentham
Probably something is wrong or your hosting provider don't support symlinkHeterophony
Nothing wrong with my hosting, it's normal that you can't reach files in root directory because we set the root directory for the domain to domain.com/public. My hosting supports symlink and I solved it through SSHBentham
The idea worked for me! It did complain about failing to create the symlink, because $_SERVER['DOCUMENT_ROOT'] returned the path including the public folder. I ended up echoing the path and manually entering and altering the variables in the symlink function, which ran successfully!Obeisance
@Obeisance Would you like to share what you did?Salsify
@Salsify I first did something like echo getcwd(); to know the path of uploaded files. I then manually entered the paths in the symlink-function, something like symlink('/path/of/existing/folder', '/path/of/preferred/folder);. It's a while back so I'm not sure about the exact things I entered.Obeisance
A
1

Try this one.

<img src="{{asset('puppy/18/ax2Fk08nyWrV6TwTOnsXNCkNGuIdFebB7TTfPYGb.png')}}" alt="">
Antifreeze answered 3/8, 2018 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.