Bruno Munhoz
Bruno Munhoz

Reputation: 53

Laravel - Images not loading in production server, work fine in local

I'm pretty new to working with laravel, and I just don't really understand how it deals with files. I followed the instructions in the documentation, used php artisan storage:link as instructed, but no go. I can't even see the images by going to their location in my url.

Here's my config/filesystems file:

<?php

return [

/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/

'default' => env('FILESYSTEM_DRIVER', 'local'),

/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/

'cloud' => env('FILESYSTEM_CLOUD', 's3'),

/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "s3", "rackspace"
|
*/

'disks' => [

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

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

    'img' => [
      'driver' => 'local',
      'root' => storage_path('images')
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

],

];

and this is how I'm calling the images in the template:

<img src="{{asset('storage/images/icons/icon.png')}}">

I've checked, and the images are physically located at ROOT/storage/app/public/images.

What am I doing wrong here? And most importantly, why does this all work just fine in my local environment and not in my production server?

For additional info: The production server is hosted by Hostgator, in a subdomain of my company's main site. I don't know if that's an issue, as I said I am new to this whole thing.

Upvotes: 2

Views: 2596

Answers (2)

Hassan Ahmad
Hassan Ahmad

Reputation: 1

This is the link issue as you are storing images in storage.

Solution 1:
if you have access to ssh then by running this command php artisan storage:link

Solution 2:

or by creating manual storage by creating temporary route

Route::get('/create-storage-link', function () {
    try {
        $target = storage_path('app/public');
        $link = public_path('storage');
        
        if (file_exists($link)) {
            return 'The storage link already exists.';
        }

        symlink($target, $link);
        return 'Storage link created successfully!';
    } catch (Exception $e) {
        return 'Error creating storage link: ' . $e->getMessage();
    }
});

Upvotes: -1

samrap
samrap

Reputation: 5673

The asset helper you are using in your Blade templates gives you the path for public assets, that is, assets located in the public folder of your Laravel application. Your static assets (such as logos, background images, etc) should be kept inside of your public directory, which is where your app is served from.

The storage directory is typically used for assets uploaded by a user in your application, such as a user uploading a new profile picture. To get the storage path, you would use the storage_path helper function to retrieve the path. Alternatively, you may use the Storage::url() method to retrieve a full URL to the storage path.

Upvotes: 1

Related Questions