Laravel - How to revert local storage symlink and refresh

前端 未结 4 1510
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 16:20

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 sto

相关标签:
4条回答
  • 2021-02-05 17:00

    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

    0 讨论(0)
  • 2021-02-05 17:04

    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.

    0 讨论(0)
  • 2021-02-05 17:04

    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.

    0 讨论(0)
  • 2021-02-05 17:17

    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
    
    0 讨论(0)
提交回复
热议问题