简体   繁体   English

浏览器不显示来自 Laravel 存储的图像

[英]Browser doesn't show image from Laravel storage

I'm using Laravel 8 with Docker on Windows 10 through WSL2 and Sail .我在 Windows 10 上通过WSL2SailLaravel 8Docker一起使用。 I created a CRUD to store some articles with images in my DB.我创建了一个 CRUD 来在我的数据库中存储一些带有图像的文章。 I'm trying to show images from laravel storage, but they are not appearing in the browser.我正在尝试显示来自 laravel 存储的图像,但它们没有出现在浏览器中。 How can I fix this?我怎样才能解决这个问题?

ArticleController.php文章控制器.php

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.articles.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'title' => 'required',
            'body' => 'required',
            'author' => 'required',
            'image' => 'nullable | image | max:1024'
        ]);

        $image = Storage::put('images', $request->image); 
        $validatedData['image'] = $image;

        Article::create($validatedData);

        return redirect()->route('admin.articles.index');
    }

I followed the Laravel 8 docs to storage my images using the public disk.我按照Laravel 8 文档使用公共磁盘存储我的图像。

filesystem.php文件系统.php

<?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', 'public'),

    /*
    |--------------------------------------------------------------------------
    | 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", "sftp", "s3"
    |
    */

    'disks' => [

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

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

        '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),
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Symbolic Links
    |--------------------------------------------------------------------------
    |
    | Here you may configure the symbolic links that will be created when the
    | `storage:link` Artisan command is executed. The array keys should be
    | the locations of the links and the values should be their targets.
    |
    */

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

I also created the link with the command line sail artisan storage:link Everything work well, because I can see the image in the right path: storage/app/images/my_image as follow:我还用命令行sail artisan storage:link创建了链接 一切正常,因为我可以在正确的路径中看到图像: storage/app/images/my_image如下:

enter image description here enter image description here在此输入图片说明 在输入图片说明

I have a problem when I try to show my image in the laravel blade as follow:当我尝试在 laravel 刀片中显示我的图像时遇到问题,如下所示:

<div class="flex justify-center flex-wrap">
    @foreach ($articles as $article)
        {{-- Articles --}}
        <div class="max-w-sm rounded overflow-hidden shadow-lg my-5 mx-2">
            <img class="w-full" src="{{ asset( 'storage/' . $article->image) }}" alt="article_image">
            <div class="px-6 py-4">
                <div class="font-bold text-xl mb-2">
                    {{ $article->title }}
                </div>
                <p class="text-gray-700 text-base overflow-hidden h-16">{{ $article->body }}</p>
                <span class="inline-block bg-gray-200 rounded-full px-3 py-1 mt-3 text-sm font-semibold text-gray-700">{{ $article->author }}</span>
            </div>
        </div>
    @endforeach
</div>

I think the problem is on the path because on the browser i can see that: <img class="w-full" src="http://localhost/storage/images/9D1GCN9XDEwRWq8zN2wiA6eBSazgg657f9uUs0Uj.png" alt="article_image"> and the image is not shown.我认为问题出在路径上,因为在浏览器上我可以看到: <img class="w-full" src="http://localhost/storage/images/9D1GCN9XDEwRWq8zN2wiA6eBSazgg657f9uUs0Uj.png" alt="article_image">和图像未显示。

I tried everything, change the filesystem, change the path storage into the controller etc etc.. but I can't find a solution.我尝试了一切,更改文件系统,将路径存储更改为控制器等。但我找不到解决方案。

As the image path in the browser looks right, I think is a problem with the symbolic link to the public directory generated with sail artisan storage:link .由于浏览器中的图像路径看起来是正确的,我认为是使用sail artisan storage:link生成的公共目录的符号链接有问题。

I had the same problem and solved it running php artisan storage:link from inside the container.我遇到了同样的问题,并在容器内部运行php artisan storage:link解决了它。

To me, the solution was making the link directly inside the container, not through sail.对我来说,解决方案是直接在集装箱内建立链接,而不是通过帆。

# Get inside the container. Replace "your-project-container" with the actual name.
docker exec -it your-project-container /bin/sh
# Change to user sail
su sail
# Run artisan storage:link
php artisan storage:link
# Exit twice to get out the container
exit
exit

Doing sail artisan storage:link creates a wrong link with a non existing /var/www/html path outside the container.执行sail artisan storage:link会创建一个错误的链接,该链接与容器外不存在的/var/www/html 路径。

I think you should put get()我认为你应该把get()

$image = Storage::put('images', $request->image->get());

If you don't define the disk like:如果您没有像这样定义磁盘:

Storage::disk('public')->put('images', $request->image->get());

it will be in default disk ("local").它将在默认磁盘(“本地”)中。 And to retrieve maybe you can put like:并检索也许你可以像这样:

<img class="w-full" src="{{ Storage::url('storage/' . $article->image) }}" alt="article_image">

A better approach would be:更好的方法是:

$path = "image_" . time() . '.' . $request->image->extension();
Storage::disk('public')->put($path, $request->image->get());
Article::create([...,
   'image'=>$path,
]);

Retrieve取回

<img class="w-full" src="{{ Storage::url($article->image) }}" alt="article_image">

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

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