skip to Main Content

On my Laravel website I’m using symlink to store and show the images from storage.

With

php artisan storage:link

I had created the symlink and everytime when I upload a new news article, the image is uploaded in the main Storage and with symlink it’s setup to the public folder and I’m displaying the image properly.

So far so good, but when I’ve created a copy of the website, a problem appears…

When I’ve created a copy of the website with cPanels File Manager, and move to a new location, the storage symlink in the public directory has become a folder, not a symlink.
After that when I try to upload a new news article, I can see it’s uploaded in the main Storage folder, but not in the public/storage, so as a result the image is not displaying. That’s because it’s not a symlink anymore, but now it’s a folder.

I’ve deleted the storage folder from the public directory, with SSH I’ve used the command again

php artisan storage:link

and I’ve created a new news article and the image is displaying properly, but now all other images are gone.

Is there any command that will regenerate the paths, so all other images will be display again?

I’m using Laravel 5.5

3

Answers


  1. Try this:

    In route/web.php add the following code:

    Route::get('/storage', function(){
        Artisan::call('storage:link');
        return "Se han vinculado las imágenes";
    });
    
    Login or Signup to reply.
    1. You can solve it in another way to create a symlinkexample.php file
      into your public folder and run the file path into browser.
    2. Then Storage folder will be created into public folder. Folder path
      will be public/storage with linked to your public folder.

    Code below for symlinkexample.php :

    <?php
        $targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
        $linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
        symlink($targetFolder,$linkFolder);
        echo 'Symlink process successfully completed';
    ?>
    
    Login or Signup to reply.
  2. If for some reason you are using different file structure and for some reason don’t have terminal access, Artisan::call('storage:link') will fail since it won’t find public path.

    Route::get('cmd', function(){
       $process = new Process(['ln', '[symlink-here]','[target-folder]' ]);
       $process->run();
    
       // executes after the command finishes
       if (!$process->isSuccessful()) {
          throw new ProcessFailedException($process);
       }
       echo $process->getOutput();
    });
    

    read about Process class here

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