skip to Main Content

I saw some questions about, people say to run this command

ln -s ../storage/app/public public/storage

And it works, it copies the folder, but it doesn’t update when another file/image is added to the storage folder, you have to run the command again to be able to get it.

How do I make it equal to Laravel, that the public folder is always ‘copying’ the storage files?

Version: "laravel/lumen-framework": "^8.3.1",
"league/flysystem": "1.1"

2

Answers


  1. First, make sure the storage/app/public folder exists. if not, please create a new one.

    Then run:

    ln -sfv ~/path/to/storage/app/public ~/path/to/public/storage
    

    Then, move files at storage/app/public

    Now you should be able to access assets like this:

    http://localhost:8000/storage/logo.png
    

    `

    Login or Signup to reply.
  2. Lumen/Laravel way to create symlink:

    app()->make('files')
        ->link(storage_path('app/public'), rtrim(app()->basePath('public/storage/'), '/'));
    

    updated. Create a command example:

    php artisan make:command FilesLinkCommand
    

    then next file will be created: app/Console/Commands/FilesLinkCommand.php

    Open it and change some thingth:

    1. better command signature:
      protected $signature = 'files:link';

    2. command description: protected $description = 'Create symlink link for files';

    3. command code, update method handle():

    public function handle(): void
    {
        // check if exists public/storage
        if ( file_exists(app()->basePath('public/storage/')) ) {
            $this->error("Symlink exists!");
            exit(0);
        }
        // check if exists storage/app/public
        if ( !file_exists(storage_path('app/public')) ) {
            $this->error("Storage folder [app/public] not exists!");
            exit(0);
        }
        // actually, we can create folders if not exists
        // using mkdir() php function, or File::makeDirectory()
        // but the second one still uses mkdir() behind the scene
    
        app()->make('files')
            ->link(storage_path('app/public'), rtrim(app()->basePath('public/storage/'), '/'));
        $this->info("Symlink created!");
    }
    

    That’s all we need!

    Using: php artisan files:link

    If command run first time you will see message Symlink created, next time it will be changed to Symlink exists!


    php function symlink() can do the same

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search