Laravel - How to revert local storage symlink and refresh
Asked Answered
L

6

27

When I save images to storage they show in the storage/app directory, but they don't show in public/storage. I also noticed that a storage/app/public directory seems to have been created which also contains everything within storage/app.

I can't work out how I managed to cause this mess, and I think it might make sense to revert these directories to how it should have been to begin with (with a new laravel project), remove any existing symlinks, and start again - does this sound like the best approach? And how would I go about doing this if I have set the symlink up incorrectly?

Labaw answered 29/1, 2018 at 21:59 Comment(1)
For anyone coding in Windows environment - check this post: laravel.at.jeffsbox.eu/laravel-5-gotchas-symbolic-link-symlinkOpuntia
C
41

You can always delete the link:

cd public
rm storage

And create a new one without using the storage:link command if you need to link different locations:

\File::link(storage_path('dir1'), public_path('dir2'));

Or create a symlink manually:

ln -s /full/path/to/storage/dir1 /full/path/to/public/dir2
Cerotype answered 29/1, 2018 at 22:6 Comment(0)
I
15

I had this problem and I fixed it by adding this to my deployment script:

cd public
rm storage
cd ..
php artisan storage:link

Before you do that, you can SSH into your deployment server and navigate to the /public folder. You might see a storage folder in there, and you should notice it's a symlink. You should leave it there and add my above commands to your deployment script.

If you remove storage link manually, you'll need to skip these on the first deployment:

cd public
rm storage
cd ..

Then every deployment after, you can do:

cd public
rm storage
cd ..
php artisan storage:link

If you don't do that, your deployment will fail probably because if rm storage throws an error, it will fail the build. Pay close attention to what I'm saying. You must be very specific when doing CLI commands, unless you can add some bash script that handles the 2 cases where ./public/storage exists (1) and when it doesn't (2).

You might gloss over my mention about deleting the storage symlink reference manually, but don't forget that if you deployment script does it and then fails for another reason, the symlink will be gone.

Someone should post an answer that uses a bash script to ensure both cases can be handled. I don't have the skill for that.

NOTE: php artisan storage:link only works while you are in the root directory of your project. That's why my above script starts with cd public and why it does cd ... If you aren't in the root at the time, the php artisan command will fail.

Interpolation answered 20/12, 2020 at 21:3 Comment(0)
I
6

Simple Way

Go to public folder Just delete/remove the storage link inside public/

Then Run the command

    php artisan storage:link

the above command will link storage again

Illyrian answered 21/9, 2021 at 9:40 Comment(0)
D
5

That is actually how it should be! Laravel has a command to symlink your public directory to the storage directory:

php artisan storage:link

The idea is to keep a persistent storage between deployments.

This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.

https://laravel.com/docs/5.5/filesystem

Dejesus answered 29/1, 2018 at 22:6 Comment(2)
I don't think this is available in Laravel 5.1Curley
Doesn't answer the questionUnsparing
M
3

For windows users, I have tested this with Windows 10 and Laravel 7

Steps:

  1. cd to your <project_root>/public directory and run rmdir storage - it will remove the link
  2. cd back to project root directory and run php artisan storage:link to link again

The links should now be refreshed and viewable without issues.

Motivation answered 2/8, 2020 at 14:53 Comment(0)
U
0
php artisan storage:link --force
Unsparing answered 29/6, 2022 at 17:51 Comment(1)
link already exists.Doth

© 2022 - 2024 — McMap. All rights reserved.