Search code examples
laravelsymlinklaravel-storage

php artisan storage:link on shared Hosting is not showing images


I have been working on a laravel app so for security reason I've placed all of my public files inside the base folder located in public_html and other files have been placed in the public_html/secondBase.

Now when I execute the command: php artisan storage:link the following error appears.

 ErrorException

  symlink(): No such file or directory

  at vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:265
    261|      */
    262|     public function link($target, $link)
    263|     {
    264|         if (! windows_os()) {
  > 265|             return symlink($target, $link);
    266|         }
    267|
    268|         $mode = $this->isDirectory($target) ? 'J' : 'H';
    269|

I tired the solution below

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

public function register()
{
    $this->app->bind('path.public', function() {
        return base_path() . '/public_html/base';
    });
}

the second method that I've tried is by making the file symlink.php, but the images are still broken.

symlink.php

<?php 
symlink('/home/hmm/public_html/secondBase/storage/app/public','/home/hmm/public_html/base/storage');

and finally I made this route where the first error appears.

Route::get('/clear', function () {
       
    Artisan::call('route:clear');
    Artisan::call('storage:link', [] );
});

Thanks for any help.


Solution

  •     Go to /public directory and run:
    
        rm storage
    
        Go to Laravel root directory and run:
    
        php artisan storage:link
    

    This problem comes when laravel project is moved/copied to some other folder.

    The storage link is still there thus causing the exception error. public/storage folder exists and points to wrong location and it needs to be deleted with rm storage command.

    After that run php artisan storage:link in terminal and it will create the storage link.

    This needs to be done EVERY time when laravel is moved/copied/deployed!