简体   繁体   English

Laravel - 找不到图像,尽管它在磁盘中。 即使通过输入 URL 也无法访问它

[英]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.所以我正在 Laravel 中构建一个应用程序,这是我第一次在没有互联网帮助的情况下独自使用该框架,所以我在某些方面遇到了很多麻烦。 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.其中之一是,即使我上传的图像被上传到文件系统中的正确位置,laravel 似乎也无法找到并提供它们。 I am working on my local environment with Valet.我正在与 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: laravel 似乎试图达到的网址是这样的:

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 :您应该只传递在 public 目录中存储文件的文件夹,而不包括public ,并且您可以在第二个参数中指定一个磁盘,如下所示:

$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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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