简体   繁体   中英

Laravel - Can't find an image, though it is there in the disk. Can't reach it even through typing the URL

So I'm building an app in Laravel and it's my first time using the framework by myself without any help aside from the internet, so I'm having quite a bit of trouble in some areas. One of them is that the images I upload seem to be impossible for laravel to find and serve, even though they are getting uploaded to the correct location in the filesystem. I am working on my local environment with Valet. Also, I have already made the symlink between storage and public as instructed by the documentation.

Here's the code that uploads the image:

$fileDir = 'public/enterprise/main';
$tempImage = request()->file('main_img');
$newImage = $tempImage->store($fileDir);

And here's how I'm trying to call it:

<div class="col-xs-8"><img src="{{ asset('storage/app/'.$enterprise->main_img) }}"></div>

The url laravel seems to be trying to reach is this:

http://vbi.dev/storage/app/public/enterprise/main/MgfjPKWL8DIFgHDrMccOCsOkZZH0QmFAsput07n5.jpeg

Finally, my config/filesystems file:

<?php
return [
    'default' => env('FILESYSTEM_DRIVER', 'local'),
    'cloud' => env('FILESYSTEM_CLOUD', 's3'),
    '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'),
        ],
    ],
];

Thank you for your attention.

You should pass only the folders where to store the file in the public directory without including the public , And you can specify a Disk inthe second argument like this :

$fileDir = 'enterprise/main';
$tempImage = request()->file('main_img');
$newImage = $tempImage->store($fileDir, 'public');

And the same thing in the view the asset method will automaticly add the path for the public folder for you :

<div class="col-xs-8"><img src="{{ asset($enterprise->main_img) }}"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM