Laravel - How to revert local storage symlink and refresh

喜欢而已 提交于 2020-12-28 18:28:16

问题


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?


回答1:


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



回答2:


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




回答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.




回答4:


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.



来源:https://stackoverflow.com/questions/48510683/laravel-how-to-revert-local-storage-symlink-and-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!