skip to Main Content

I have a Laravel 9 project and I just uploaded this project into server.

Now for this project everything works fine except the images that are coming from storage directory.

For example, I tried creating and storing the captcha images at storage/app/dist/captcha/{ip_address_of_user}/.

Now when I check this directory, I can see that the captcha images are properly created and stored there.

But in the view, this code can not load the images:

<div class="row" style="margin-left:50px" dir="ltr">
    @foreach(range(0,5) as $i)
        <div class="col-2">
            <img src="{{ asset("storage/dist/captcha/".$ip."/".$i."captcha.png") }}">
        </div>
    @endforeach
</div></br>

Here is my filesystems.php:

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/public_html/storage',
            'visibility' => 'public',
            'throw' => false,
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
        ],

        'dist' => [
            'driver' => 'local',
            'root' => storage_path('app/dist'),
            'url' => env('APP_URL') . '/storage/app',
            'visibility' => 'public',
            'throw' => false,
        ],

        'upload' => [
            'driver' => 'local',
            'root' => storage_path('app/upload'),
            'url' => env('APP_URL') . '/storage/app',
            'visibility' => 'public',
            'throw' => false,
        ],
    ],

With these links :

'links' => [
        public_path('storage') => storage_path('app/public'),
        public_path('storage/dist') => storage_path('app/dist'),
        public_path('storage/upload') => storage_path('app/upload'),
    ],

I tried running php artisan storage:link in the command line of my server, but this error returns:

INFO  The [public/storage] link has been connected to [storage/app/public].


   ErrorException

  symlink(): No such file or directory

  at vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:340
    336▕      */
    337▕     public function link($target, $link)
    338▕     {
    339▕         if (! windows_os()) {
  ➜ 340▕             return symlink($target, $link);
    341▕         }
    342▕
    343▕         $mode = $this->isDirectory($target) ? 'J' : 'H';
    344▕

      +15 vendor frames
  16  artisan:35
      IlluminateFoundationConsoleKernel::handle()

Screenshot Update:

enter image description here

I don’t know really what’s going wrong here!

So if you know how to solve this problem or how to load the images from storage directory , please help me out with this…

Note that I changed the public directory to public_html and added this to AppServiceProvider:

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

3

Answers


  1. Hope it can help you

    <img src="{{ asset("storage/app/dist/captcha/".$ip."/".$i."captcha.png") }}">
    
    Login or Signup to reply.
  2. If you want a quick fix, just bind your path.public in bootstrap/app.php instead of AppServiceProvider.

    Edit bootstrap/app.php

    ...
    
    /*
    |--------------------------------------------------------------------------
    | Return The Application
    |--------------------------------------------------------------------------
    |
    | This script returns the application instance. The instance is given to
    | the calling script so we can separate the building of the instances
    | from the actual running of the application and sending responses.
    |
    */
    
    // Bind your public path here.
    $app->bind('path.public',function(){
        return base_path() . '/public_html';
    });
    
    return $app;
    

    Then the php artisan storage:link command should be work fine.

    For the reason why, you can find more comment from here:https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5

    Login or Signup to reply.
  3. you changed public directory to public_html and set this in AppServiceProvider , but this command don’t set in config/filesystems.php , you must edit link part to this :

     'links' => [
        base_path() . '/public_html/storage' => storage_path('app/public'),
        base_path() . '/public_html/storage/dist' => storage_path('app/public/dist'),
        base_path() . '/public_html/storage/upload' => storage_path('app/public/upload'),
    ],
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search