上传的图片未加载 Laravel 7

Uploaded images not loading Laravel 7

我的 laravel 站点有一个功能,我可以将表单数据提交到数据库并将提交的图像保存在存储目录中。数据正确存储在数据库中,但不知何故图像无法加载,我不知道该怎么做,因为这已经困扰我几天了。

如果有人能看到并告诉我我做错了什么,那就太好了!

我创建了一个符号链接使用 php artisan link:storage

filesystems.php

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

PostController.php 具有存储功能:

public function store(Request $request)
{

    request()->validate([
        'title' => ['required', 'string', 'max:255',],
        'thumbnail' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048|unique:posts',
        'iframe' => ['required', 'string', 'max:255',],
    ]);

    $image = $request->file('thumbnail');
    $originalname = $image->getClientOriginalName();

    Storage::disk('public')->put($originalname, file_get_contents($image));


    Post::create([
        'title' => $request['title'],
        'thumbnail' => $originalname,
        'iframe' => $request['iframe'],
    ]);

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

这就是我尝试加载图像的方式:

<img width="100px" src="{{asset('storage/public/'.$post->thumbnail)}}">

This is where my image is saved This is the path in the browser

您是否在表单元素上添加了 enctype 属性?

如果没有添加下面===> enctype="multipart/form-data"

我认为问题出在 html 中的 <image> 标签。

<img src="{{ Storage::url("/storage/app/{$post->thumbnail}") }}" alt="{{ $post->filename }}" />

有关 Retrieving Files from storage

的更多详细信息,请参阅 laravel 文档

我发现了问题所在...不知何故我的符号链接无法正常工作。上传仅在我的 storage/app/public 而不是在我的 public/storage 文件夹中,我删除了 public/storage 文件夹并创建了一个新的符号链接。

正在删除目录:

rm public/storage

创建新的符号链接:

php artisan storage:link